Computer vision practice

Library introducing here is open cv2

  • pip install opencv
In [4]:
import tensorflow as tf
tf.__version__
Out[4]:
'2.1.0'
In [5]:
import numpy as np

import matplotlib.pyplot as plt
from PIL import Image
In [6]:
import warnings
warnings.filterwarnings('ignore')
In [7]:
pic = Image.open('c:/Users/Hari/Desktop/datasets/Naresh_datasets/00-puppy.jpg')
In [8]:
pic
Out[8]:
In [13]:
# what type of object 
print(type(pic))
<class 'PIL.JpegImagePlugin.JpegImageFile'>
In [14]:
# converting the image into array
pic_arr = np.asarray(pic)
In [15]:
type(pic_arr)
Out[15]:
numpy.ndarray
In [16]:
pic_arr # here number of dimension represet number of square brackst
Out[16]:
array([[[95, 81, 78],
        [97, 83, 80],
        [98, 84, 81],
        ...,
        [25, 27, 22],
        [25, 27, 22],
        [25, 27, 22]],

       [[95, 81, 78],
        [96, 82, 79],
        [96, 82, 79],
        ...,
        [25, 27, 22],
        [25, 27, 22],
        [25, 27, 22]],

       [[95, 81, 78],
        [94, 80, 77],
        [94, 80, 77],
        ...,
        [25, 27, 22],
        [25, 27, 22],
        [25, 27, 22]],

       ...,

       [[19, 29, 20],
        [20, 30, 21],
        [20, 30, 21],
        ...,
        [23, 30, 22],
        [24, 31, 23],
        [24, 31, 23]],

       [[20, 30, 21],
        [20, 30, 21],
        [19, 29, 20],
        ...,
        [23, 30, 22],
        [24, 31, 23],
        [24, 31, 23]],

       [[20, 30, 21],
        [19, 29, 20],
        [19, 29, 20],
        ...,
        [23, 30, 22],
        [24, 31, 23],
        [24, 31, 23]]], dtype=uint8)
  • color image has three dimension i.e 3rd dimenion is color channel that is RGB

  • blackwhite image has only two dimension i.e intensity represent the black color

In [18]:
pic_arr.shape 
Out[18]:
(1300, 1950, 3)

with help of the plt.imshow() we can display image in the form of numpy array

In [19]:
#displaying the image
plt.imshow(pic_arr)
print(pic_arr.shape)
# previous diffrent is while open with Image function is no pixel scale
# if you open with matplotlib.pyplot.imshow it will open with along with pixel scale

# please notice here axis not like regular axis, it's not start with both axis at zero point
(1300, 1950, 3)
In [20]:
print(type(pic_arr))
<class 'numpy.ndarray'>
In [21]:
pic_red = pic_arr.copy()
In [22]:
plt.imshow(pic_red)
Out[22]:
<matplotlib.image.AxesImage at 0x1f1d972bec8>
In [23]:
pic_red.shape
Out[23]:
(1300, 1950, 3)
In [14]:
pic_red[:,:,0] # values are changed 
# This will access the 
# red cahnnel notice the vals are not btween 0&255
Out[14]:
array([[95, 97, 98, ..., 25, 25, 25],
       [95, 96, 96, ..., 25, 25, 25],
       [95, 94, 94, ..., 25, 25, 25],
       ...,
       [19, 20, 20, ..., 23, 24, 24],
       [20, 20, 19, ..., 23, 24, 24],
       [20, 19, 19, ..., 23, 24, 24]], dtype=uint8)
In [35]:
plt.imshow(pic_red[:,:,0])
Out[35]:
<matplotlib.image.AxesImage at 0x1f1d93233c8>

This is internally virdis color scale by matplot

In [36]:
plt.imshow(pic_red[:,:,1]) # 1 --> indicates the green channels
Out[36]:
<matplotlib.image.AxesImage at 0x1f1d960e748>
In [37]:
plt.imshow(pic_red[:,:,2]) # 2 --> indicates the blue channels
Out[37]:
<matplotlib.image.AxesImage at 0x1f1d9684d88>

This is internally virdis color scale by matplot

In [38]:
# here explicitly we mentioning color mapping is black and white
plt.imshow(pic_red[:,:,0],cmap='gray')
#Bw,camp is colormap, gray scale
# 0 mean no red,pure black
# notice the color of the ears of the dog,no red,  they are light red channel values r btween 0-255
Out[38]:
<matplotlib.image.AxesImage at 0x1f1e0106d48>
In [39]:
plt.imshow(pic_red)
#notice the color of the dog,slightly brownish read
Out[39]:
<matplotlib.image.AxesImage at 0x1f1e03fcd08>
In [40]:
# ax,fig = plt.subplot(121)
plt.figure(figsize=(15,15))
plt.imshow(pic_red[:,:,1]) # thir
plt.show()
In [41]:
plt.figure(figsize=(15,15))
plt.imshow(pic_red[:,:,-1]) # thir
plt.show()
# This is for green,notice the ears, green is gone, they are slight dark
In [42]:
#  This is for green channel
plt.imshow(pic_arr[:,:,2])
Out[42]:
<matplotlib.image.AxesImage at 0x1f1e1156fc8>

This is G channel and zeroing the green channe

In [43]:
pic_red[:,:,1] = 0
pic_red.shape
Out[43]:
(1300, 1950, 3)
In [46]:
print(pic_red.shape)
plt.imshow(pic_red)
# notice it looks purplsh, why, only b & r rleft
(1300, 1950, 3)
Out[46]:
<matplotlib.image.AxesImage at 0x1f1e129cfc8>
In [47]:
# take out the blue color
pic_red[:,:,2] = 0
pic_red.shape
Out[47]:
(1300, 1950, 3)
In [48]:
# notice nothing is showing here without colorchannel
plt.imshow(pic_red)
Out[48]:
<matplotlib.image.AxesImage at 0x1f1e17dce48>
In [49]:
# take out the blue color 
# Their is only three color channels cv2 i.e BGR B-0,G-1,R-2 if make zero a
pic_red[:,:,0] = 0
pic_red.shape
Out[49]:
(1300, 1950, 3)
In [50]:
# notice it is redhs
plt.imshow(pic_red)
Out[50]:
<matplotlib.image.AxesImage at 0x1f1e184a348>

working with open CV

  • install required libraries
  • !pip install opencv
  • python -m pip install --upgrade
    • !pip install opencv
In [52]:
import numpy as np
import cv
import matplotlib.pyplot as plt
# get_ipython().magic('%matplotlib inline')
In [53]:
import cv2
img = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/00-puppy.jpg')
In [54]:
img.shape
Out[54]:
(1300, 1950, 3)
In [55]:
plt.imshow(img)
# notice slightly bluish, why
Out[55]:
<matplotlib.image.AxesImage at 0x1f1e41639c8>

OCV & matplotlib have defferent format of RGB channels

  • matplotlib filles in as RGB
  • OCV fills as BGR
  • The image has been correctly loaded by opencv as a numpy arr, but hte color of each pixel has been sordted as BGR

  • matplotlib's plot expectes an RGB image so, for a correct display of the image, # swap those channels

  • This can be done by using openCV conversion function cv2.cvtColot() or by working directly with the numpy array.

  • we have different color space models
    • RBG
    • BGR
    • HSV
    • HSL
In [56]:
# This will convert to BGR to RGB color format
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()
In [57]:
img_gray = cv2.imread('c:/Users/hari/desktop/datasets/Naresh_datasets/paret01.jpg')
# img_gray

Dt: 15/12/2019

In [58]:
plt.imshow(img_gray)
Out[58]:
<matplotlib.image.AxesImage at 0x1f1e49918c8>
In [59]:
img_gray = cv2.imread('c:/Users/hari/desktop/datasets/Naresh_datasets/00-puppy.jpg')
In [60]:
plt.imshow(img_gray)
# why it is not in Bw, becasueof the order of channels
Out[60]:
<matplotlib.image.AxesImage at 0x1f1e4ba4e48>
In [63]:
cv2.imread?
Docstring:
imread(filename[, flags]) -> retval
.   @brief Loads an image from a file.
.   
.   @anchor imread
.   
.   The function imread loads an image from the specified file and returns it. If the image cannot be
.   read (because of missing file, improper permissions, unsupported or invalid format), the function
.   returns an empty matrix ( Mat::data==NULL ).
.   
.   Currently, the following file formats are supported:
.   
.   -   Windows bitmaps - \*.bmp, \*.dib (always supported)
.   -   JPEG files - \*.jpeg, \*.jpg, \*.jpe (see the *Note* section)
.   -   JPEG 2000 files - \*.jp2 (see the *Note* section)
.   -   Portable Network Graphics - \*.png (see the *Note* section)
.   -   WebP - \*.webp (see the *Note* section)
.   -   Portable image format - \*.pbm, \*.pgm, \*.ppm \*.pxm, \*.pnm (always supported)
.   -   PFM files - \*.pfm (see the *Note* section)
.   -   Sun rasters - \*.sr, \*.ras (always supported)
.   -   TIFF files - \*.tiff, \*.tif (see the *Note* section)
.   -   OpenEXR Image files - \*.exr (see the *Note* section)
.   -   Radiance HDR - \*.hdr, \*.pic (always supported)
.   -   Raster and Vector geospatial data supported by GDAL (see the *Note* section)
.   
.   @note
.   -   The function determines the type of an image by the content, not by the file extension.
.   -   In the case of color images, the decoded images will have the channels stored in **B G R** order.
.   -   When using IMREAD_GRAYSCALE, the codec's internal grayscale conversion will be used, if available.
.       Results may differ to the output of cvtColor()
.   -   On Microsoft Windows\* OS and MacOSX\*, the codecs shipped with an OpenCV image (libjpeg,
.       libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs,
.       and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware
.       that currently these native image loaders give images with different pixel values because of
.       the color management embedded into MacOSX.
.   -   On Linux\*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for
.       codecs supplied with an OS image. Install the relevant packages (do not forget the development
.       files, for example, "libjpeg-dev", in Debian\* and Ubuntu\*) to get the codec support or turn
.       on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
.   -   In the case you set *WITH_GDAL* flag to true in CMake and @ref IMREAD_LOAD_GDAL to load the image,
.       then the [GDAL](http://www.gdal.org) driver will be used in order to decode the image, supporting
.       the following formats: [Raster](http://www.gdal.org/formats_list.html),
.       [Vector](http://www.gdal.org/ogr_formats.html).
.   -   If EXIF information are embedded in the image file, the EXIF orientation will be taken into account
.       and thus the image will be rotated accordingly except if the flag @ref IMREAD_IGNORE_ORIENTATION is passed.
.   -   Use the IMREAD_UNCHANGED flag to keep the floating point values from PFM image.
.   -   By default number of pixels must be less than 2^30. Limit can be set using system
.       variable OPENCV_IO_MAX_IMAGE_PIXELS
.   
.   @param filename Name of file to be loaded.
.   @param flags Flag that can take values of cv::ImreadModes
Type:      builtin_function_or_method
In [61]:
img_gray = cv2.imread('c:/Users/hari/desktop/datasets/Naresh_datasets/00-puppy.jpg',
                     cv2.IMREAD_GRAYSCALE)
In [62]:
plt.imshow(img_gray)
Out[62]:
<matplotlib.image.AxesImage at 0x1f1e4c146c8>
In [65]:
plt.imshow(img_gray,cmap='gray') #cmap = 'gray' means convert to Black white image
Out[65]:
<matplotlib.image.AxesImage at 0x1f1e4901548>
In [66]:
plt.imshow(img_gray,cmap='magma')
plt.show()
In [68]:
print(img_gray.min())
print(img_gray.max())
1
209

Reisze image & img manipulation

In [69]:
plt.imshow(img_rgb) # this is the original img
plt.show()
img_rgb.shape
Out[69]:
(1300, 1950, 3)

width, height, color channels

Docstring: resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst . @brief Resizes an image. . . The function resize resizes the image src down to or up to the specified size. Note that the . initial dst type or size are not taken into account. Instead, the size and type are derived from . the `src`,`dsize`,`fx`, and `fy`. If you want to resize src so that it fits the pre-created dst, . you may call the function as follows: . @code . // explicitly specify dsize=dst.size(); fx and fy will be computed from that. . resize(src, dst, dst.size(), 0, 0, interpolation); . @endcode . If you want to decimate the image by factor of 2 in each direction, you can call the function this . way: . @code . // specify fx and fy and let the function compute the destination image size. . resize(src, dst, Size(), 0.5, 0.5, interpolation); . @endcode . To shrink an image, it will generally look best with #INTER_AREA interpolation, whereas to . enlarge an image, it will generally look best with c#INTER_CUBIC (slow) or #INTER_LINEAR . (faster but still looks OK). . . @param src input image. . @param dst output image; it has the size dsize (when it is non-zero) or the size computed from . src.size(), fx, and fy; the type of dst is the same as of src. . @param dsize output image size; if it equals zero, it is computed as: . \f[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\f] . Either dsize or both fx and fy must be non-zero. . @param fx scale factor along the horizontal axis; when it equals 0, it is computed as . \f[\texttt{(double)dsize.width/src.cols}\f] . @param fy scale factor along the vertical axis; when it equals 0, it is computed as . \f[\texttt{(double)dsize.height/src.rows}\f] . @param interpolation interpolation method, see #InterpolationFlags . . @sa warpAffine, warpPerspective, remap
In [84]:
img = cv2.resize(img_rgb,(1000,400)) # resize it, this squeeze it
plt.imshow(img) # notice X-axis,y-axis change as per above mentioned
Out[84]:
<matplotlib.image.AxesImage at 0x1f1e52ab9c8>
In [85]:
img = cv2.resize(img_rgb,(1375,275))
plt.imshow(img)
Out[85]:
<matplotlib.image.AxesImage at 0x1f1e530dcc8>

Resize by ration

In [89]:
w_ratio = 0.8
h_ratio = 0.2

img = cv2.resize(img_rgb,(0,0),img,w_ratio,h_ratio) # img --> is the destination name
plt.imshow(img) # 80% smaller 
Out[89]:
<matplotlib.image.AxesImage at 0x1f1e7017848>
In [90]:
w_ratio = 0.5
h_ration = 0.5

img_new = cv2.resize(img_rgb,(0,0),img_new,w_ratio,h_ratio)
plt.imshow(img_new) # 50% smaller than originall image
Out[90]:
<matplotlib.image.AxesImage at 0x1f1e707cb88>
In [91]:
print(img_new.shape)
print(img_rgb.shape)
(260, 975, 3)
(1300, 1950, 3)

flipping images

In [100]:
get_ipython().magic('matplotlib inline')
In [101]:
# along centeral axis 
new_img = cv2.flip(img_new,0) # 0 is harizontal axis
plt.imshow(new_img)
Out[101]:
<matplotlib.image.AxesImage at 0x1f1d92d53c8>
In [102]:
# along centeral axis 
new_img = cv2.flip(img_new,1) # 1 is vartical axis
plt.imshow(new_img)
Out[102]:
<matplotlib.image.AxesImage at 0x1f1e9aed808>

Along both axis

In [103]:
new_img1 = cv2.flip(img_new,-1)
plt.imshow(new_img1)
Out[103]:
<matplotlib.image.AxesImage at 0x1f1e9b86ac8>

Saving the files

In [104]:
type(new_img1)
Out[104]:
numpy.ndarray
In [105]:
cv2.imwrite('my_first_img_writing.png',new_img1)
Out[105]:
True

The above stored the BGR version of the image Larger Display in the Notebook,as the Noteboook displays smapller one by default

In [112]:
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
ax1 = fig.add_subplot(122)

ax.imshow(new_img1)
ax1.imshow(img_rgb)
Out[112]:
<matplotlib.image.AxesImage at 0x1f1ea54ea48>
In [113]:
fig = plt.figure(figsize=(2,2))
ax = fig.add_subplot(111)
ax.imshow(img_new)
Out[113]:
<matplotlib.image.AxesImage at 0x1f1ea6407c8>

Till now worked function are

  • cv2.imread()
  • cv2.resize()
  • cv2.filp()
  • cv2.imshow()
  • cv2.imwrite()
  • cv2.waitkey()
  • cv2.putText()

Simple function which are used in the opencv2

In [114]:
import os
import cv2
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/00-puppy.jpg',
                 cv2.IMREAD_GRAYSCALE)

while True:
    # Show the image with opencv
    cv2.imshow('MyPuppy',img)
    # Wait for sometime on keyboard to be pressed to close window
    # Wait for 1ms & if ESC key is pressed 
    if (cv2.waitKey() & 0xFF == 27):
        break
        
cv2.destroyAllWindows()        

help(cv2)

Drawing on images

Dt: 16/12/2019

In [117]:
import numpy as np
import cv
blank_img = np.zeros(shape=(512,512,3),dtype=np.int16)
#give shape 512,512
blank_img
print(blank_img.shape)
# Notice here 0s -> is black and 1 is white
(512, 512, 3)
In [116]:
from matplotlib import pyplot as plt
%matplotlib inline

plt.imshow(blank_img)
print(blank_img.shape)
(512, 512, 3)

shapes and rectangels

img is image *pt1 Vertex of the rectagle

  • pt2 vertex of the rectangel opposite to pt1

  • color: rectangle color or brightness(grayscaleimge)

  • Thinkness: thickness of lines that makes up the rectangel. Negative vlaues like : FILLED ,mean that the function has to draw
      a filled rectange
  • line Type : Type of line see: type of lines

  • shift numbers: shift number of fractional bits in the points

  • pt1 = top left corner,x1,y1

  • pt2 = bottom right corner x2,y2

  • Here we are giving two poinst

In [60]:
cv2.rectangle(blank_img,pt1 = (384,0),pt2 =(512,128),
             color=(0,255,0),thickness = 5)
Out[60]:
array([[[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        ...,
        [  0, 255,   0],
        [  0, 255,   0],
        [  0, 255,   0]],

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        ...,
        [  0, 255,   0],
        [  0, 255,   0],
        [  0, 255,   0]],

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        ...,
        [  0, 255,   0],
        [  0, 255,   0],
        [  0, 255,   0]],

       ...,

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        ...,
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]],

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        ...,
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]],

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        ...,
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]]], dtype=int16)
In [61]:
plt.imshow(blank_img)
# See here the both ponts are the opposite corners is point(512 on the x-axis,128 -> on y-axis )
Out[61]:
<matplotlib.image.AxesImage at 0x1a968a3dec8>
In [64]:
cv2.rectangle(blank_img,pt1 = (384,0),pt2 =(512,128),
             color=(0,255,0),thickness = 10)
plt.imshow(blank_img)
Out[64]:
<matplotlib.image.AxesImage at 0x1a964c8afc8>

Noitce here the thickness moves from the middle to right

In [65]:
cv2.rectangle(blank_img,pt1 = (384,0),pt2 =(512,128),
             color=(0,255,0),thickness = 20)
plt.imshow(blank_img)
Out[65]:
<matplotlib.image.AxesImage at 0x1a9689ca308>

DRaw blue rectanlge in the middle of the image

  • pt1 = top left
  • pt2 = b
In [66]:
cv2.rectangle(blank_img,pt1=(200,200),pt2 = (300,300),color = (0,255,0),thickness=5)
plt.imshow(blank_img)
Out[66]:
<matplotlib.image.AxesImage at 0x1a969214708>

Circles

  • need pass the
  • center
  • radius
  • color
In [67]:
cv2.circle(img=blank_img,center=(100,100),radius=50,
           color=(255,0,0),thickness=8)
plt.imshow(blank_img)
Out[67]:
<matplotlib.image.AxesImage at 0x1a969271fc8>
In [68]:
#filled in 
cv2.circle(img=blank_img,center=(100,400),radius=20,color=(255,0,0),thickness=-1)
plt.imshow(blank_img)
# notice here thickness is zero, then it filled by color
Out[68]:
<matplotlib.image.AxesImage at 0x1a9692d5e88>

lines

  • Draw a blue line diagonal with thickness 5 px
  • Notice here we passsed only one point that is pt1 = (0,0) it will taken automatically opposite points
In [69]:
cv2.line(blank_img,pt1 = (0,0),pt2 = (511,511),
         color = (102,255,255),thickness = 5)
plt.imshow(blank_img)
Out[69]:
<matplotlib.image.AxesImage at 0x1a96933c588>

Write tex over image

  • text : Here mention text required
  • org : it's position of text where to place
  • fontFace: type of font
  • fontscale : font size
  • color :
  • thickness:
  • lineType : specified line type as per opencv formate +
In [70]:
n_img = cv2.imread('C:/Users/Hari/Desktop/datasets/Naresh_datasets/paret01.jpg')

font = cv2.FONT_HERSHEY_PLAIN
cv2.putText(n_img,text = 'Hello hari',org=(10,500),fontFace = font,
           fontScale=10,color = (255,0,255),thickness=5,
           lineType = cv2.LINE_AA)
plt.imshow(n_img)
Out[70]:
<matplotlib.image.AxesImage at 0x1a9693a1748>

Polygons

  • to draw a polygon, first we need coordinates of vertices
  • Make those points into array shape rows x1x2 where rows are number of vertces and should be of type int2
In [71]:
blank_img = np.zeros(shape=(512,512,3),dtype = np.int32) 
# Creating the black color sample image
In [72]:
vertices = np.array([[100,300],[200,200],[400,300],[200,400]],np.int32)
# Create vertice for polygon
In [73]:
vertices
Out[73]:
array([[100, 300],
       [200, 200],
       [400, 300],
       [200, 400]])
In [74]:
vertices.shape
Out[74]:
(4, 2)
In [75]:
pts = vertices.reshape((-1,1,2 )) # add third dimension i.e 1
In [76]:
cv2.polylines(blank_img,[pts],isClosed=True,color=(255,0,0),thickness=5)
plt.imshow(blank_img)
Out[76]:
<matplotlib.image.AxesImage at 0x1a969409208>

Direct Drawing with mouse dynamically not statically(physically) Might be sould run as .py script

In [77]:
import cv2
import numpy as np
# Creat a function based on a cv2 event(left botton click)
def draw_circle(event,x,y,flags,param):
    pass # this function nothing will do here
# This names the window so we can reference it 
cv2.namedWindow(winname='my_art')
# Connect mouse botton to our callback function
cv2.setMouseCallback('my_art',draw_circle)
# Create a blank image
img = np.zeros((512,512,3),np.uint8)

# Runs forever until we break with ESC key on keyboard
while True:
# show the image window
    cv2.imshow('my_art',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
# Once the script is done, its usally good to call this ,it closes
# all windows (just in case you have multiple windows called)

cv2.destroyAllWindows()
#ESC to close

Dt:17/12/2019

Draw the blue color circles with mouse left click

here problem is the indenation, i.e Funtion is identical and rest

  • define function
  • create black object and window, mouse event
  • Run while until receive respones from the user
In [ ]:
import numpy as np
import cv2
# function is identical the creating image and 
def draw_circle(event,x,y,flags,param): # x,y are passed from setmousecallback
    if event == cv2.EVENT_LBUTTONDOWN: # when the left mouse is clicked down
        cv2.circle(img,(x,y),100,(0,255,0),-1)

#***Below lines will create the square image and windows and MOuse call back function
#create a black image
img = np.zeros((512,512,3),np.uint8)
        # this name is the window so we can reference it 
cv2.namedWindow(winname='my_drawing')
        # connects the mouse bottn to our callback function
cv2.setMouseCallback('my_drawing',draw_circle)

#*****This while will continue until getting respones as ESC key from the user******

while True: #Runs forever until we break with Esc key on keyboard
    # Shows the image window
    cv2.imshow('my_drawing',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
        # Once script is done, its usually good practice to call this line
        # It closes all windows (just in case you have multiple windows called)
cv2.destroyAllWindows()

Draw circle on widnow with mouse right and left clicks

In [ ]:
# V3 
# ## Adding Functionality with Event Choices 
import cv2 
import numpy as np 
 
# Create a function based on a CV2 Event (Left button click) 
def draw_circle(event,x,y,flags,param): 
    if event == cv2.EVENT_LBUTTONDOWN: 
        cv2.circle(img,(x,y),100,(0,255,0),-1) 
    elif event == cv2.EVENT_RBUTTONDOWN: 
        cv2.circle(img,(x,y),100,(0,0,255),-1) 
 
# Create a black image
img = np.zeros((512,512,3), np.uint8) 
# This names the window so we can reference it  
cv2.namedWindow(winname='my_drawing') 
# Connects the mouse button to our callback function 
cv2.setMouseCallback('my_drawing',draw_circle) 
 
while True: #Runs forever until we break with Esc key on keyboard 
    # Shows the image window 
    cv2.imshow('my_drawing',img) 
    if cv2.waitKey(20) & 0xFF == 27: # ESC
        break 
cv2.destroyAllWindows() 

Dt: 17/12/2019

Adding functionality with event Choice

In [ ]:
import cv2
import numpy as np
def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN: # add a event to the down[LEFT click DOWN]
        cv2.circle(img,(x,y),100,(0,255,0,255),-1) 
# img is image,(x,y is coordinate capture by the mouse events),100 ---> radius in pixel
#color param(0,255,0), -1 --> represent the filling color
    elif event == cv2.EVENT_RBUTTONDOWN:
        cv2.circle(img,(x,y),100,(0,0,255),-1)
        
# second block create black image
img = np.zeros((512,512,3),np.uint8)
# named window reference
cv2.namedWindow(winname='my_art')
cv2.setMouseCallback('my_art',draw_circle)

while True:
    cv2.imshow('my_art',img)
    if cv2.waitKey(20) & 0xFF== 27:
        break
cv2.destroyAllWindows()

Dragging with mouse

  • Indentaion is mouse expected errors, loop notations are the most freqent errors in python
In [ ]:
import numpy as np
import cv2
# create a function based on cv2 event(Left botton click)
drawing = False
ix,iy = -1,-1 # to keep track
# Mouse callback function
def draw_rectangle(event,x,y,flags,param):
    global drawing,ix,iy,mode
    
    # create a rectange mouse event
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
    # after we have to track the mouse points
        ix,iy = x,y


    elif event == cv2.EVENT_MOUSEMOVE:
        # Now the mouse is moving
        if drawing == True:
        # if drawing is true it means you've already clicked on the left botton on mouse
            ix,iy = x,y
        # we draw a rectangle from the previous poistion x,y
        cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
    # ix,iy is the starting points and x,y is end points(x-coordinate,y-coordinate)
    # color- is color representation i.e green
    # -1 === represent the filling the color

    elif event == cv2.EVENT_LBUTTONUP:
        # once you lift the mouse left botton,drawing is need to finished
        drawing = False

    cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)

# Recall above defined function for drawing a rectange   
img = np.zeros((512,512,3),np.uint8)
cv2.namedWindow(winname='my_art2')
cv2.setMouseCallback('my_art2',draw_rectangle)
while True:
    cv2.imshow('my_art2',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()
        

Image processing technique

In [ ]:
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
img1 = cv2.imread('c:/Users/Hari/Desktop/Hari.jpg')
plt.imshow(img1)
In [ ]:
cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # GBR to RGB
plt.imshow(img)

converting to HSV

In [ ]:
img3 = cv2.cvtColor(img1,cv2.COLOR_BGR2HSV) # BGR to HSV
plt.imshow(img3)
In [ ]:
img2 = cv2.cvtColor(img1,cv2.COLOR_BGR2HLS) # BGR to HLS
plt.imshow(img2)

Blending and Pasting image

  • RGB to BGR
In [ ]:
# two images 
img01 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/dog_backpack.png')
plt.imshow(img01)
In [ ]:
img02 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/watermark_no_copy.png')
plt.imshow(img02)

BGR to RGB is out put is RED dominent colors

In [ ]:
img01 = cv2.cvtColor(img01,cv2.COLOR_BGR2RGB)
plt.imshow(img01)
print(img01.shape)
In [ ]:
img02 = cv2.cvtColor(img02,cv2.COLOR_BGR2RGB)
plt.imshow(img02)
print(img02.shape)

Resizing the images

In [ ]:
img1_s = cv2.resize(img01,(1200,1200))
plt.imshow(img1_s)
In [ ]:
img2_s = cv2.resize(img02,(1200,1200))
plt.imshow(img2_s)

Blending the image

  • Image overlay over other image
    • gamma deals with brightness of image
  • coverting color image or black/white
  • reducing the noise
  • key poinst: alpha,gamma
  • bledning image size should be same
In [ ]:
print(img01.shape)
print(img02.shape)

Notice back pack image has ligher backgroup

src1 = img1_s,alpha = 0.7,src2 = img2_s,beta = 0.5,gamma = 5

In [ ]:
blended = cv2.addWeighted(src1 = img1_s,alpha = 0.7,src2 = img2_s,beta = 0.5,gamma = 5)
plt.imshow(blended)
plt.show()

Notice back pack image has ligher backgroup

src1 = img1_s,alpha = 0.8,src2 = img2_s,beta = 0.1,gamma = 0.5

In [ ]:
blended1 = cv2.addWeighted(src1 = img1_s,alpha = 0.8,src2 = img2_s,beta = 0.1,gamma = .5)
plt.imshow(blended)
plt.show()
In [ ]:
blended1 = cv2.addWeighted(src1 = img1_s,alpha = 0.8,src2 = img2_s,beta = 0.1,gamma = 10)
plt.imshow(blended) # Light projection
plt.show()

Overlaying image of Different sizes

dt: 20/12/2019

  • overaly large image on a small img
  • it's not blednig
  • trick to overlap different size images
  • by simply reassigning the larger image's valeus matchs the smaller image
  • Load two images
In [ ]:
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
im1 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/dog_backpack.jpg')
im2 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/watermark_no_copy.png')

im2 = cv2.resize(im2,(600,600))
im1 = cv2.cvtColor(im1,cv2.COLOR_BGR2RGB)
im2 = cv2.cvtColor(im2,cv2.COLOR_BGR2RGB)

print(plt.imshow(im1))
# plt.imshow(im2)
In [ ]:
plt.imshow(im2)
In [ ]:
# create a region of interset
print(im1.shape)
print(im2.shape)
In [ ]:
# we can take any coordinates
x_offest = 934 - 600 # 934 is the width of larger image and 600 is width of smaller image
y_offest = 1401 - 600 # 1401 is the height of the larger image 
y_offest
In [ ]:
print(im2.shape)
In [ ]:
# createing an ROI of the same size of the foreground image
# (smaller image that will go on top)
rows,cols,channels = im2.shape # tuple unpacking
In [ ]:
# create ROI
roi = im1[y_offest:1401,x_offest:943] # this the bottom right hand corner
roi.shape
plt.imshow(roi)
In [ ]:
# Next we create a mask 
# create a mask of logo and create its inverse mask also

im2gray = cv2.cvtColor(im2,cv2.COLOR_BGR2GRAY) # conversion image 
plt.imshow(im2gray)
display(im2gray.shape)
In [ ]:
plt.imshow(im2gray,cmap='gray') # orginal image 
In [ ]:
# inverse the colors 
mask_inv = cv2.bitwise_not(im2gray) # just inverse of the yellow colored image 
plt.imshow(mask_inv)
cv2.imwrite('mask_inverse.png',mask_inv)
In [ ]:
# convert black and white 
plt.imshow(mask_inv,cmap='gray')
mask_inv.shape # color channel is removed we already know that b/w image does't have color channels
In [ ]:
# convert mask to have three channles
import numpy as np 
white_background = np.full(im2.shape,255,dtype = np.uint8)
plt.imshow(white_background) # notice here it's white image, we override with white color 
In [ ]:
# bitwise_or operation 
bk = cv2.bitwise_or(white_background,white_background,mask = mask_inv) 
# parameter for bitwize_or # bitwise_or(src1, src2[, dst[, mask]]) -> dst
plt.imshow(bk)
bk.shape
In [ ]:
## Grab Original Foreground image and place on top of mask
plt.imshow(mask_inv,cmap='gray')
In [ ]:
fg = cv2.bitwise_or(im2,im2,mask=mask_inv)
plt.imshow(fg)
fg.shape # three channles image
# observe the difference mask_inv, and im2() ![](watermark_no_copy.png) ![](mask_inverse.png)
In [ ]:
# Get ROI and blend in the mask with the ROI
print(roi.shape)
plt.imshow(roi)
In [ ]:
final_roi = cv2.bitwise_or(roi,fg)
plt.imshow(final_roi)
cv2.imwrite('btwise_or.png',final_roi)
In [ ]:
# checking the functionality
final_roi1 = cv2.bitwise_and(roi,fg)
plt.imshow(final_roi1)
cv2.imwrite('btwise_and.png',final_roi1)
In [ ]:
# checking the functionality 
final_roi2 = cv2.bitwise_xor(roi,fg)
plt.imshow(final_roi2)
cv2.imwrite('btwise_xor.jpg',final_roi2)
In [ ]:
# Now add the rest of the image 
large_image = im1
small_image = final_roi
large_image[y_offest:y_offest + small_image.shape[0],x_offest:x_offest + small_image.shape[1]] = small_image
plt.imshow(small_image)
plt.imshow(large_image)
plt.show()

Below code is just copied and pasted due to lack of time

In [ ]:
# *** Image thresholding *** #
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
img = cv2.imread('c:/users/hari/Desktop/datasets/Naresh_datasets/rainbow.jpg')
plt.imshow(img)
plt.show()
print('shape before removeing zero flag',img.shape)

# Adding the 0 flag to read it in black and white
img = cv2.imread('c:/users/hari/Desktop/datasets/Naresh_datasets/rainbow.jpg',0) # makeing color channel as zero, no color channel mean b/w
plt.imshow(img,cmap='gray')# this is gray scale image
plt.show()
print('shape after removeing zero flag/removeing color channel',img.shape)

Here, the matter is straight-forward.

**For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value.**

In [ ]:
# ## Different Threshold Types
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

#******************
#threshold(src, thresh, maxval, type[, dst]) -> retval, dst
#******************

# thresholding is where certain vals r cutoff ie lt r assigned below 0
# and others abv r 1
ret
# 127 is min
# 255 is max pssbl thrshld

img.max() # this max val in the img
display(ret)
plt.imshow(thresh1,cmap='gray')
plt.show()
# this is binary img, all r either 0 or 255
# 0 is blk
print('thresolt: ',ret)
print('max values pixel: ',thresh1.shape)
# plt.imshow(ret,cmap='gray') retthresh1

Binary Inverse

In [ ]:
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
plt.imshow(thresh2,cmap='gray')
plt.show()
In [ ]:
# ### Threshold Truncation
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
plt.imshow(thresh3,cmap='gray')
plt.show()

some of them keep the orignl val and others get down back to the threshld

if it is abv cutoff it is threshlding it

otherwise it keeps it

In [ ]:
# ### Threshold to Zero
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
plt.imshow(thresh4,cmap='gray')
plt.show()
In [ ]:
# ### Threshold to Zero (Inverse of abv)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
plt.imshow(thresh5,cmap='gray')
plt.show()

Adaptive Thresholding

The function transforms a grayscale image to a binary image according to the formulae:

THRESH_BINARY dst(x,y) = \fork{\texttt{maxValue}}{if $src(x,y) > T(x,y)$}{0}{otherwise}

THRESH_BINARY_INV dst(x,y) = \fork{0}{if $src(x,y) > T(x,y)$}{\texttt{maxValue}}{otherwise}

In [ ]:
# # Real World Applications
# ## Adaptive Thresholding
# ### Sudoku Image
img = cv2.imread("c:/Users/Hari/Desktop/datasets/Naresh_datasets/crossword.jpg",0)
plt.imshow(img)
plt.show()

# After removing the color channels
plt.imshow(img,cmap='gray')
plt.show()
In [ ]:
# To 
In [ ]:
def show_pic(img):
    fig = plt.figure(figsize=(15,15))
    ax = fig.add_subplot(111)
    ax.imshow(img,cmap='gray')
show_pic(img)
print(img.shape)
In [ ]:
# ## Simple Binary
ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
show_pic(th1)
display(th1.shape)
#v loose sm of the quality, sm of the gray spaces get thrlded to white
# but v want them to keep as blk

# image quality is dropped, i.e below thersold 127 pixel value set to zero 

# incr the thrshld
ret,th1 = cv2.threshold(img,200,255,cv2.THRESH_BINARY)
show_pic(th1)

in case simple thersold:

we used one global value as a threshold. But this might not be good in all cases, e.g. if an image has different lighting conditions in different areas. In that case, adaptive thresholding can help.*

  • Here, the algorithm determines the threshold for a pixel based on a small region around it. So we get different thresholds for different regions of the same image which gives better results for images with varying illumination.

Whereas the conventional thresholding operator uses a global threshold for all pixels, adaptive thresholding changes the threshold dynamically over the image. This more sophisticated version of thresholding can accommodate changing lighting conditions in the image, e.g. those occurring as a result of a strong illumination gradient or shadows.

In [ ]:
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,8)
# it will look around the neighborhood of pixl vals
# the blk squares r not filled in due to thrshld/cutoff unlike binary thrsld
# Play with last 2 numbers
show_pic(th2)
In [ ]:
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,15,8)
show_pic(th3)
In [ ]:
blended = cv2.addWeighted(src1=th1,alpha=0.7,src2=th2,beta=0.3,gamma=0)
show_pic(blended)
# this is a blend of dfrnt thrshlods

***Blurring & Smoothing

Image blurring is achieved by convolving the image with a low-pass filter kernel. It is useful for removing noise. It actually removes high frequency content (e.g: noise, edges) from the image resulting in edges being blurred when this is filter is applied. (Well, there are blurring techniques which do not blur edges). OpenCV provides mainly four types of blurring techniques.

  • Averaging
  • Gausssian burr
  • Median filltering
  • Bilateral Blurring
In [ ]:
# ## Convenience Functions
# function for loading the puppy image.
import warnings
warnings.filterwarnings('ignore')

def load_img():
    img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/bricks.jpg').astype(np.float32) / 255
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img

def display_img(img):
    fig = plt.figure(figsize=(12,10))
    ax = fig.add_subplot(111)
    ax.imshow(img)
i = load_img()
display_img(i)

Gamma Correction : Practical Effect of Increasing Brightness

In [ ]:
img = load_img()
gamma = 1/4 #gamma is 0.25
effected_image = np.power(img, gamma)
display_img(effected_image)
# Gamma Corr is abt increasing/decreasing the brightness effect
# the img is brighter if gamma < 1
In [ ]:
gamma = 1/10 # gamma = 0.1
effected_image = np.power(img, gamma)
display_img(effected_image)
# img fades more
In [ ]:
gamma = 2 # gamma is not decimal so it is higher brightness
effected_image = np.power(img, gamma)
display_img(effected_image)
# img fades more 
In [ ]:
gamma = 8
effected_image = np.power(img, gamma)
display_img(effected_image)
# almost black
In [ ]:
# Blurring
# ### Low Pass Filter with a 2D Convolution
# A fitlering operation known as 2D convolution can be used to create
# a low-pass filter.
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)
#org is the position
In [ ]:
# setup the K for LPF

# ### Create the Kernel
1/25 # 0.04
kernel = np.ones(shape=(5,5),dtype=np.float32)/25
kernel
dst = cv2.filter2D(img,-1,kernel)
# -1 is neg depth
display_img(dst)
# ntc the spacing in letters is pink & filled

Averaging

In [ ]:
# bck to orgnl img
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)
# this is the orgnl img
In [ ]:
# cv2.blur(src, ksize[, dst[, anchor[, borderType]]])


blurred_img = cv2.blur(img,ksize=(5,5)) # this is default cv2s built in kernel
display_img(blurred_img)
In [ ]:
blurred_img = cv2.blur(img,ksize=(10,10)) # this is default cv2s built in kernel
display_img(blurred_img)
In [ ]:
# ## Gaussian Blurring
# tks a group of pxls calc avg or median then mk them the outputs
# this the fresh img
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX

# cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)

GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) -> dst

In [ ]:
blurred_img = cv2.GaussianBlur(img,(5,5),10)

display_img(blurred_img)

## Median Blurring

In [ ]:
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)

medianBlur(src, ksize[, dst]) -> dst

In [ ]:
median = cv2.medianBlur(img,5)
display_img(median)
# ntc k in bricks it is not tht much blurrd as in gaussian blur
# it is more clear this is removing noise from text

### Adding Noise

In [ ]:
# a more useful case of Median Blurring by adding some random noise to an image.
img = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/sammy.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
display_img(img)

print(img.max())

print(img.min())
print(img.mean())
print(img.shape)

# by default image is noisey only

noise_img = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/sammy_noise.jpg') # this is noisy img
display_img(noise_img)

medianBlur(src, ksize[, dst]) -> dst

In [ ]:
median = cv2.medianBlur(noise_img,5) # we achived better image from noise image
display_img(median)
# this looks btr than the noisy img although not as clear as the org img

## Bilateral Filtering

In [ ]:
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
# cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)

As we noted, the filters we presented earlier tend to blur edges. This is not the case for the bilateral filter, cv2.bilateralFilter(), which was defined for, and is highly effective at noise removal while preserving edges. But the operation is slower compared to other filters. We already saw that a Gaussian filter takes the a neighborhood around the pixel and finds its Gaussian weighted average. This Gaussian filter is a function of space alone, that is, nearby pixels are considered while filtering. It does not consider whether pixels have almost the same intensity value and does not consider whether the pixel lies on an edge or not. The resulting effect is that Gaussian filters tend to blur edges, which is undesirable.

In [ ]:
blur = cv2.bilateralFilter(img,9,75,75)

display_img(blur)
# the img has blurrd but the txt is slightly clear
# it is in between median blur & mean
# the obj is to reduce the noise or the detail in the img

*** Morphological operators

They r specialzed kernels to achieve special effects

In [ ]:
def load_img():
    blank_img =np.zeros((600,600))
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(blank_img,text='ABCDE',org=(50,300), fontFace=font,fontScale=5,color=(255,255,255),thickness=25,lineType=cv2.LINE_AA)
    return blank_img

def display_img(img):
    fig = plt.figure(figsize=(6,5))
    ax = fig.add_subplot(111)
    ax.imshow(img,cmap='gray')
    
img = load_img()
display_img(img)
  • We will learn different morphological operations like Erosion, Dilation, Opening, Closing etc.
  • We will see different functions like : cv.erode(), cv.dilate(), cv.morphologyEx() etc.

all the pixels near boundary will be discarded depending upon the size of kernel. So the thickness or size of the foreground object decreases or simply white region decreases in the image. It is useful for removing small white noises (as we have seen in colorspace chapter), detach two connected objects etc.

In [ ]:
# ## Erosion
# Erodes away boundaries of foreground objects.
# Works when foreground is light color (preferrably white) and background is dark.
kernel = np.ones((5,5),np.uint8)
# create the kernel
erosion1 = cv2.erode(img,kernel,iterations = 1)
display_img(erosion1)
# FG is white, BG is black
# ntc the cnnection btw A & B is slightly weak compared to the orgnl

img = load_img()
kernel = np.ones((5,5),np.uint8)
erosion5 = cv2.erode(img,kernel,iterations = 4)
display_img(erosion5)
# cnnctns r getting eroded away
In [ ]:
# ## Opening
# Opening is erosion followed by dilation. Used in removing background noise!
img = load_img()
white_noise = np.random.randint(low=0,high=2,size=(600,600))
white_noise
# arr of pts 0 & 1 of size 600 x 600

display_img(white_noise)

img.max()
# ntc 0s and 1s

# now add the white noise into our txt img
white_noise = white_noise*255
white_noise
# this is the same scale of orgl img
In [ ]:
# now add the white noise into our txt img
white_noise = white_noise*255
white_noise
# this is the same scale of orgl img

display_img(white_noise)
In [ ]:
# now add the white noise into our txt img
noise_img = white_noise+img
display_img(noise_img)
# img of dark bg & light fg w a lot of noise

morphological operator

Morphological operators often take a binary image and a structuring element as input and combine them using a set operator (intersection, union, inclusion, complement). They process objects in the input image based on characteristics of its shape, which are encoded in the structuring element. The mathematical details are explained in Mathematical Morphology.

morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst

In [ ]:
opening = cv2.morphologyEx(noise_img, cv2.MORPH_OPEN, kernel,)
display_img(opening)
# ntc v r able to remove the noise wo distorting the orgnl img
# this is for remving the bg noise
#if v now see the orgl img
display_img(img)
# dilation is expanding on img
# ### Closing
# Useful in removing noise from foreground objects, as black dots on top of the
# white text.
In [ ]:
# ### Closing
# Useful in removing noise from foreground objects, as black dots on top of the
# white text.
In [ ]:
img = load_img() # reload/reset the img
display_img(img)

black_noise = np.random.randint(low=0,high=2,size=(600,600))
black_noise

display_img(black_noise) # looks same as white noise

black_noise= black_noise * -255
black_noise # ntc the neg vals it will not affect the black but white fg
In [ ]:
display_img(black_noise) # ntc the white fg is filled with noise

# add blck nose to the img
black_noise_img = img + black_noise
display_img(black_noise_img)
display_img(noise_img) # this is white noise

#black_noise = np.random.randint(low=0,high=2,size=(600,600))
#black_noise

#display_img(black_noise_img)
In [ ]:
# v will try to mk the neg vals to zeros, as the pxls r from 0 to 255
black_noise_img[black_noise_img==-255] = 0
black_noise_img
display_img(black_noise_img)
In [ ]:
# closing is a process to clean up the FG
# opening is a process to clean up he BG
closing = cv2.morphologyEx(black_noise_img, cv2.MORPH_CLOSE, kernel)
display_img(closing)
In [ ]:
# ## Morphological Gradient
# Difference between dilation and erosion of an image.
img = load_img()

display_img(img)

# erosion tries to remove the edges of alphabet and blck bg
# dilation tries to add around the edges and mkae it more bubbly
# gradient will tk the dfrnt btw the 2
gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
display_img(gradient)
# this is a simple process of edge detection
# this is the dfrnt btw erosion & dilation

*** Gradients

is an extension of morphological operators, it helps in sohpistciated opers such as edge detection

which r used in object detection, tracking & image classification,In gradient the color changes from black to white and viceversa

can b tracked using algs

In [ ]:
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sudoku.jpg',0)

def display_img(img):
    fig = plt.figure(figsize=(6,5))
    ax = fig.add_subplot(111)
    ax.imshow(img,cmap='gray')

display_img(img)
In [ ]:
# this is x gradeint Sobel
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
# cv2.CV_64F is pxl precision
# 1 is derivative x (dx)
# 0 is derivative y (dy)
In [ ]:
# ksize is an odd num v can chang this
display_img(sobelx)
# vert lins r clear
# it ds not erase any hor lines
In [ ]:
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
display_img(sobely)
# ntc hor lines r clear
In [ ]:
# this is the laplacean of the img
# 2nd der of x & 2nd der of y
laplacian = cv2.Laplacian(img,cv2.CV_64F)
display_img(laplacian)
# ntc it tries to do edge detection wrt to x & y
# it gets rid of sm noise
# ntc the edges around 8 went frm white to black to white
# nd the nums r clear
# ex of an appli whi will look at a sudoku pic
# nd auto fill in sm nums

later some more things need to be practice but now time left to practice, i will it later

Blening Images :

  • dt- 24/12/2019
  • for combining multiple things, i.e sobel & x gradient,sobel & y gradeints
In [ ]:
blended = cv2.addWeighted(src1=sobelx,alpha = 0.5,src2 = sobely,beta = 0.5,gamma=0)
display_img(blended)
display(blended.shape)

# this blended result of xgrad,ygrad,edges are more clear
# we can also combine thersolding & morphological oerators
In [ ]:
# using morp oper 
# ### Morphological Operators 
In [ ]:
kernel = np.ones((4,4),np.uint8) 
gradient = cv2.morphologyEx(blended,cv2.MORPH_GRADIENT,kernel) 
display_img(gradient) 
# this gvs b & w result 
# this is used for edge detection 
# In edge detection all the techniqs r combined and called 

this gvs b & w result

this is used for edge detection In edge detection all the techniqs r combined and called

In [ ]:
# more combinations 
# Try it on laplacian result 
kernel = np.ones((3,3),np.uint8) 
gradient = cv2.morphologyEx(blended,cv2.MORPH_GRADIENT,kernel) 
display_img(gradient) 
 
In [ ]:
 
# ### Thresholds 
ret,th1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY) 
display_img(th1) 
 
In [ ]:
 
ret,th1 = cv2.threshold(gradient,200,255,cv2.THRESH_BINARY_INV) 
display_img(th1) 
  
In [ ]:
ret,th1 = cv2.threshold(blended,100,255,cv2.THRESH_BINARY_INV) 
display_img(th1) 
 
 

HIstograms

  • histogram of equlaization
In [ ]:
#Orginal image
dark_horse = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/horse.jpg')
plt.imshow(dark_horse)

ntc a lot of black around ,v dont see bgr

In [ ]:
# its repesent exact releastic image in RGB mode 
show_horse = cv2.cvtColor(dark_horse,cv2.COLOR_BGR2RGB)
plt.imshow(show_horse)
In [ ]:
rainbow= cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/rainbow.jpg')
plt.imshow(rainbow)

in place of blue color filled rad color

In [ ]:
show_rainbow = cv2.cvtColor(rainbow,cv2.COLOR_BGR2RGB)
# display(show_rainbow)
plt.imshow(show_rainbow)
In [ ]:
blue_bricks = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/bricks.jpg') 
plt.imshow(blue_bricks) 
plt.show() 
 
In [ ]:
# ### OpenCV Histogram 
# **cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])** 
# * images : it is the source image of type uint8 or float32.  
# it should be given in square brackets, ie, “[img]”. 
# * channels : it is also given in square brackets.  
# It is the index of channel for which we calculate histogram. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively. 
# * mask : mask image.  
# To find histogram of full image, it is given as “None”. But if you want to find histogram of particular region of image, you have to create a mask image for that and give it as mask. (I will show an example later.) 
# * histSize : this represents our BIN count. 
# Need to be given in square brackets. For full scale, we pass [256]. 
# * ranges : this is our RANGE. Normally, it is [0,256]. 
In [ ]:
hist_values = cv2.calcHist([blue_bricks],channels =[0],histSize=[256],mask=None,ranges=[0,256])
hist_values.shape
plt.imshow(hist_values)
plt.show() 
In [ ]:
plt.imshow(show_horse) 
plt.show() 
 
plt.imshow(dark_horse) 
plt.show() 
 

below hist represnt the blue channel i.e 0 will have more valeus

In [ ]:
hist_values = cv2.calcHist([dark_horse],channels=[0],mask=None,histSize=[256],ranges=[0,256]) 
plt.plot(hist_values) 
plt.show() 

most of the vals for blue chnnl r 0

coz most of the img is black

nd the horse is brown

remembr blk is 0 pxl and white is 1

In [ ]:
# ## Plotting 3 Color Histograms 
img = blue_bricks 
color = ('b','g','r') 
for i,col in enumerate(color): 
    histr = cv2.calcHist([img],[i],None,[256],[0,256]) 
    plt.plot(histr,color = col) 
    plt.xlim([0,256]) 
plt.title('Blue Bricks Image') 
plt.show() 
plt.imshow(img)

zoom x and y axis

calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist

. @overload T

In [ ]:
for i,col in enumerate(color): 
    histr = cv2.calcHist([img],[i],None,[256],[0,256]) 
    plt.plot(histr,color = col) 
    plt.xlim([0,50]) 
plt.ylim([0,500000]) 
plt.title('Dark Horse') 
plt.show() 
plt.imshow(img)
In [ ]:
img = rainbow 
color = ('b','g','r') 
for i,col in enumerate(color): 
    histr = cv2.calcHist([img],[i],None,[256],[0,256]) 
    plt.plot(histr,color = col) 
    plt.xlim([0,256]) 
plt.title('Rainbow Image') 
plt.show() 
plt.imshow(img)
    
In [ ]:
# ### Masking 
# We can mask only certain parts of the image. 
img = rainbow 
img.shape 
 

 
# create a mask 
 
img.shape[:2] 
# same as above 
 
mask = np.zeros(img.shape[:2], np.uint8) 
mask 
 
plt.imshow(mask) 
plt.show() 
 
plt.imshow(mask,cmap='gray') 
plt.show() 
 
 
In [ ]:
mask[300:400, 100:400] = 255 
# slice a rect from the img 
# then set it to 255 whi is wht 
 
plt.imshow(mask,cmap='gray') 
 
In [ ]:
plt.imshow(show_rainbow) 
# this is color corrected 
 
masked_img = cv2.bitwise_and(img,img,mask = mask) 
# this is used for hist calc 
 
# create a mask in the rainbow 
# this is used for rgb color calc 
show_masked_img = cv2.bitwise_and(show_rainbow,show_rainbow,mask = mask) 
 
 
plt.imshow(show_masked_img) 
# ntc not much red is here cmprd to orgl img 
 
In [ ]:
 
hist_mask_values_red = cv2.calcHist([rainbow],channels=[2],mask=mask,histSize=[256],ranges=[0,256]) 
# rainbow is orgnl img 
# 2 is red channl in b g r 
# mask is the obj v created on top 
 
plt.plot(hist_mask_values_red) 
plt.title('Histogram for RED values for the Masked Area') 
# v low vals for red & most of the vals r 0 
 
plt.imshow(show_rainbow) 
plt.show() 
# ntc a lot of red 
 
In [ ]:
hist_full_values_red = cv2.calcHist([rainbow],channels=[2],mask=None,histSize=[256],ranges=[0,256]) 
plt.plot(hist_full_values_red) 
plt.title('Histogram for RED values of the full image') 
plt.show() 
# ntc a lot of vals for red 
In [ ]:
## Histogram Equalization 
In [ ]:
gorilla = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/gorilla.jpg',0) 
 
def display(img,cmap=None): 
    fig = plt.figure(figsize=(5,4)) 
    ax = fig.add_subplot(111) 
    ax.imshow(img,cmap) 
 

 
display(gorilla) 
In [ ]:
# Calc histogram, then equalize it, then visualize the dfrnc, then run on color version 
hist_values = cv2.calcHist([gorilla],channels=[0],mask=None,histSize=[256],ranges=[0,256]) 
In [ ]:
# Calc histogram, then equalize it, then visualize the dfrnc, then run on color version 
hist_values = cv2.calcHist([gorilla],channels=[0],mask=None,histSize=[256],ranges=[0,256]) 
# channels[0] is 0 coz it is gray there is only one channel 
plt.plot(hist_values) 
plt.show() 
# looks like v hv lot of lighter color and few darker but not much black  
# if v look at the img the lighter colors r coming frm the rock 
# bhnd gorilla and gor is also a little lighter nd not compleely dark 
# the peak is the lighter gray and as v mv to rhs is the darker colr 
 
In [ ]:
 
 
# this is a ver of gor 
eq_gorilla = cv2.equalizeHist(gorilla) 
display(eq_gorilla,cmap='gray') 
# ntc the contrast has increased 
# the lighter vals now r min vals of 0 
# nd the darker vals r black whi r the near the hair of gor 
# nd the edges of rock 
In [ ]:
hist_values = cv2.calcHist([eq_gorilla],channels=[0],mask=None,histSize=[256],ranges=[0,256]) 
plt.plot(hist_values) 
# ntc a lot of spikes going down coz of 0 vals 
# a lot of lighter vals hv cm 0 
# whereas the orgnl one is more flat 
In [ ]:
 
 
# ## redo the abv for Color Image 
color_gorilla = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/gorilla.jpg') 
display(color_gorilla) 
 
 
In [ ]:
 
show_gorilla = cv2.cvtColor(color_gorilla,cv2.COLOR_BGR2RGB) 
display(show_gorilla) 
 
In [ ]:
 
# Convert to HSV colorspace to equlaize a color img 
# equzli increass the color contrast 
# hue sat val 
hsv = cv2.cvtColor(color_gorilla, cv2.COLOR_BGR2HSV) 
display(show_gorilla) 
 
 
In [ ]:
 
# Grab V channel 
hsv[:,:,2] 
# vals chnnl is wht v r interested in 
# 0 chnnl is the hue 
# 1 chnnl s the sat 
 
hsv[:,:,2].max() # max val 
 
hsv[:,:,2].min()  
 
 
# equalize val chnnl, then replace the orgnl vals 
hsv[:,:,2] = cv2.equalizeHist(hsv[:,:,2]) 
 
 
# Convert back hsv to RGB to visualize 
eq_color_gorilla = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) 
display(eq_color_gorilla) 
# compare this to orgnl v hv higher contrast 
# ntc the rock bhnd is much lighter compared to the orgnl 

Cv2 part_2

Video processing Dt: 25/12/2019

  • Note: It's copied pasted code
  • Not well grasping the code

video writing below functionality not implemented

In [ ]:
# *** Video Processing 
# when running video files mk sure only 1 kernel is running 
# connecting to the camera 
import cv2 
# Connects to your computer's default camera 
cap = cv2.VideoCapture(0) 
# a VC is a series of imgs 
# a grp of imgs getting updtd continually 
 
# Get the width and height from video feed 
# (returns float which we need to convert to integer for later) 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 
while True: 
    # Capture frame-by-frame 
    ret, frame = cap.read() 
 # tuple unpack 
    # Our operations on the frame come here 
 # convert it to gray frame 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    # Display the resulting frame 
    cv2.imshow('frame',gray) 
    # This command let's us quit with the "q" button on a keyboard. 
    # Simply pressing X on the window won't work! 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break 
# When everything done, release the capture and destroy the windows 
cap.release() # stop capturing the video 
cv2.destroyAllWindows() 
 

 

Writting a video stream to file

In [ ]:
import cv2 
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
writer = cv2.VideoWriter('my_v.mp4',cv2.VideoWriter_fourcc(*'DIVX'),25,(width,height))

while True:
    ret,frame = cap.read()
    
    cv2.imshow('frame',frame)
    writer.write(frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
writer.release()
cv2.destroyAllWindows()
    
# ## Writing a Video Stream to File # ### Notebook : Make sure its all in the same cell! # FourCC is a 4-byte code used to specify the video codec. # The list of available codes can be found in fourcc.org. It is platform dependent. # INFO ON CODECS: https://docs.opencv.org/3.0beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#saving-a-video cap = cv2.VideoCapture(0) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # MACOS AND LINUX: *'XVID' (MacOS users try VIDX ) # WINDOWS *'VIDX' writer = cv2.VideoWriter('my_capture.mp4', cv2.VideoWriter_fourcc(*'DIVX'),25, (width, height)) #writer = cv2.VideoWriter('my_capture.mp4', cv2.VideoWriter_fourcc(*'XVID'),25, (width, height)) # cv2.VideoWriter_fourcc(*'XVID') is the video codec # ths is dfrnt from ur OS # fourcc is a 4 byte code used to specify video codec # 25 is the num of frames/sec v want to record # the more the frames/sec the largr the file ## This loop keeps recording until you hit Q or escape the window ## instead use some sort of timer, like from time import sleep and then just record for 5 seconds. while True: # Capture frame-by-frame ret, frame = cap.read() # Write the video writer.write(frame) # Display the resulting frame cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() writer.release() cv2.destroyAllWindows()
In [ ]:
# # OpenCV with Video Files 
# open the recorded video from the last ,  
# you can use this code to open any major video format. 
# Run everything in one cell! 
import cv2 
import time 
# Same command function as streaming, its just now we pass in the file path, nice! 
cap = cv2.VideoCapture('my_v.mp4') 
# FRAMES PER SECOND FOR VIDEO 
fps = 25 
 
# check if the video was acutally there 
# If you get an error at thsi step, triple check your file path!! 
if cap.isOpened()== False:  
    print("Error opening the video file. Please double check your file path for typos. Or move the movie file to the same location as this script/notebook") 
# While the video is opened 
while cap.isOpened(): 
    # Read the video file. 
    ret, frame = cap.read() 
    # If we got frames, show them. 
    if ret == True: 
        # Display the frame at same frame rate of recording 
        #time.sleep(1/fps) 
        cv2.imshow('frame',frame) 
        # Press q to quit 
        if cv2.waitKey(25) & 0xFF == ord('q'): 
            break 
    # Or automatically break this whole loop if the video is over. 
    else: 
        break 
cap.release() 
cv2.destroyAllWindows() 
In [ ]:
import time
# wht is the dfrnc 
cap = cv2.VideoCapture('my_v.mp4') 
# fps = 25
fps = 100
if cap.isOpened()== False:  
    print("Error opening the video file. Please double check your file path for typos. Or move the movie file to the same location as this script/notebook") 
while cap.isOpened(): 
    ret, frame = cap.read() 
    if ret == True: 
        time.sleep(1/fps) 
        cv2.imshow('frame',frame) 
        if cv2.waitKey(100) & 0xFF == ord('q'): # Here waitkey until wait key. frame will run
            break 
    else: 
        break 
cap.release() 
cv2.destroyAllWindows() 
In [ ]:
# # Drawing on Video 
# to analyze video using techniques like object detection or facial recognition,  
# we want to draw an image on the video, like a box around a face. 
cap = cv2.VideoCapture(0) 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 
 
# using // here because Python // allows for int classical division,  
# because we can't pass a float to the cv2.rectangle function 
# Coordinates for Rectangle, top left corner 
x = width//2 
y = height//2 
# Width and height 
 
w = width//4 
h = height//4 
# botton right x+w, y+h 
 
while True: 
    # Capture frame-by-frame 
    ret, frame = cap.read() 
 
 # Draw a rectangle on stream 
    cv2.rectangle(frame, (x, y), (x+w, y+h), color=(0,0,255),thickness= 4) 
  
 # Display the resulting frame 
    cv2.imshow('frame', frame) 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break 
cap.release() 
cv2.destroyAllWindows() 
 
 
In [ ]:
# ## Interactive Drawing on Video 
# Create a function based on a CV2 Event (Left button click) 
# mouse callback function 
def draw_rectangle(event,x,y,flags,param): 
    global pt1,pt2,topLeft_clicked,botRight_clicked 
 # get mouse click 
    if event == cv2.EVENT_LBUTTONDOWN: 
        if topLeft_clicked == True and botRight_clicked == True: 
            topLeft_clicked = False 
            botRight_clicked = False 
            pt1 = (0,0) 
            pt2 = (0,0) 
 
        if topLeft_clicked == False: 
            pt1 = (x,y) 
            topLeft_clicked = True 
             
        elif botRight_clicked == False: 
            pt2 = (x,y) 
            botRight_clicked = True 
         
# Global vars 
pt1 = (0,0) 
pt2 = (0,0) 
topLeft_clicked = False 
botRight_clicked = False 
 
# Connect to the callback 
cap = cv2.VideoCapture(0)  
# Create a named window for connections 
cv2.namedWindow('Test') 
 
# Bind draw_rectangle function to mouse cliks 
cv2.setMouseCallback('Test', draw_rectangle)  
 
while True: 
    # Capture frame-by-frame 
    ret, frame = cap.read() 
     
    if topLeft_clicked: 
        cv2.circle(frame, center=pt1, radius=5, color=(0,0,255), thickness=-1) 
     
    #drawing rectangle 
    if topLeft_clicked and botRight_clicked: 
        cv2.rectangle(frame, pt1, pt2, (0, 0, 255), 2) 
     
    # Display the resulting frame 
    cv2.imshow('Test', frame) 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break 
cap.release() 
cv2.destroyAllWindows() 

Object detection

In [ ]:
full = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy.jpg')
full = cv2.cvtColor(full,cv2.COLOR_BGR2RGB)
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(full)

Template Image

  • A subset of the image. its actucally the exact image
In [ ]:
face = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy_face.jpg')
face = cv2.cvtColor(face,cv2.COLOR_BGR2RGB)
plt.imshow(face)
print('full sammy shape:' ,full.shape)
print('face sammy shape: ',face.shape)
In [ ]:
import cv2
# # Template Matching Methods 
# **eval()** function  
sum([1,2,3]) 
 
mystring = 'sum' 
 
eval(mystring) 
 
myfunc = eval(mystring) 
 
myfunc([1,2,3]) 
 
height, width,channels = face.shape 
print(width) 
print(height) 
In [ ]:
#The full image to search 
# large image 
full = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/sammy.jpg')
full = cv2.cvtColor(full,cv2.COLOR_BGR2RGB)

# The template to match 
#small image
import matplotlib.pyplot as plt
%matplotlib inline
face = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy_face.jpg')   
face = cv2.cvtColor(face,cv2.COLOR_BGR2RGB)    

my_method = eval('cv2.TM_CCOEFF')
res = cv2.matchTemplate(full,face,my_method)

plt.imshow(res)

All the 6 methods for comparision in a list

we are using strings, we'll use the eval() function to convert to function

In [ ]:
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR','cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] 
import matplotlib as mpl
for m in methods:
    full_copy = full.copy()
    method = eval(m)
    res = cv2.matchTemplate(full_copy,face,method)
    min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res)
    # draw rectangel
    if method in [cv2.TM_SQDIFF or cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
        
    height,width,channels = face.shape
    
    # assign bottom right of the rectangle
    bottom_right = (top_left[0] + width, top_left[1] + height)
    
    # Draw red rectangle 
#     plt.figure(figsize=(16,8))
#     mpl.rcParams(figsize(16,6))
    plt.rcParams['figure.figsize'] = (16,8)
    plt.subplot(121)
    plt.imshow(res)
    plt.title('Result of template matching')
    
    plt.subplot(122)
    plt.imshow(full_copy)
    plt.title('Detected point')
    plt.suptitle(m)
    
    plt.show()
    print('\n')
    print('\n')

Corner detection

In [ ]:
import cv2 
from matplotlib import pyplot as plt
%matplotlib inline

flat_chess = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/flat_chessboard.png')
flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB)

# cvLoadImage(flat_chess, CV_LOAD_IMAGE_GRAYSCALE);
plt.imshow(flat_chess)
print('\n')
In [ ]:
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB)
# gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COL)
plt.imshow(gray_flat_chess)
In [ ]:
real_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/real_chessboard.jpg')
real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB)
plt.imshow(real_chess)
plt.imshow(real_chess,cmap='gray')
real_chess_bw = cv2.cvtColor(real_chess,cv2.COLOR_RGB2GRAY)
plt.imshow(real_chess_bw)

gray_real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_real_chess,cmap='gray')
# # Harris Corner Detection # **cornerHarris Function** # * src Input single-channel 8-bit or floating-point image. # * dst Image to store the Harris detector responses. It has the type CV_32FC1 and the same size as src . # * blockSize Neighborhood size (see the details on #cornerEigenValsAndVecs ). # * ksize Aperture parameter for the Sobel operator. # * k Harris detector free parameter. See the formula in DocString # * borderType Pixel extrapolation method. See #BorderTypes
In [ ]:
gray_flat_chess
import numpy as np
gray = np.float32(gray_flat_chess)
gray_flat_chess in gray

Corner Harris Detection

  • convert gray scale image to float values
  • result is dialted for marking the corners, not importent to actual corner detection
In [ ]:
gray_flat_chess
import numpy as np
gray = np.float32(gray_flat_chess)
# gray_flat_chess in gray
In [ ]:
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png')
plt.imshow(flat_chess)
plt.show()

flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_RGB2BGR)
plt.imshow(flat_chess)
plt.show()
In [ ]:
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_flat_chess)
In [ ]:
# # Corner Detection 
# ### The Image Data 
 
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png') 
flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB) 
plt.imshow(flat_chess) 
 
 
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2GRAY) 
plt.imshow(gray_flat_chess,cmap='gray') 
_
In [ ]:
 
real_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/real_chessboard.jpg') 
real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB) 
plt.imshow(real_chess) 
 
 
gray_real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY) 
plt.imshow(gray_real_chess,cmap='gray') 
 
In [ ]:
 
# # Harris Corner Detection 
# **cornerHarris Function** 
# *  src Input single-channel 8-bit or floating-point image. 
# *  dst Image to store the Harris detector responses. It has the type CV_32FC1 and the same size as src . 
# *  blockSize Neighborhood size (see the details on #cornerEigenValsAndVecs ). 
# *  ksize Aperture parameter for the Sobel operator. 
# *  k Harris detector free parameter. See the formula in DocString 
# *  borderType Pixel extrapolation method. See #BorderTypes. 
 
 
# all integers 
gray_flat_chess 

 
# Convert Gray Scale Image to Float Values 
gray = np.float32(gray_flat_chess) 
gray  
 
# Corner Harris Detection 
dst = cv2.cornerHarris(src=gray,blockSize=2,ksize=3,k=0.04) 
# blksize is the neighborhood size, it detects edges 
# ksize is the par for sobel oper, kernel size 
# cornerHarris uses these internally 
# k is the harris detector free par 
dst 
 
 
In [ ]:
# result is dilated for marking the corners, not important to actual corner detection 
# this is so we can plot out the points on the image shown 
dst = cv2.dilate(dst,None) 
# dilate is a morphological oper 
dst 

 
# Threshold for an optimal value, it may vary depending on the image. 
flat_chess  
 
 
flat_chess[dst>0.01*dst.max()]=[255,0,0]  
# whenever the corner harris is gt 1% of max val 
# reassign 1% of max val to color red 
# this is for visualziation 
flat_chess  
 
plt.imshow(flat_chess) 
# ntc all corners r not detected 
 
 
In [ ]:
 

# try on real chess board 
# Convert Gray Scale Image to Float Values 
gray = np.float32(gray_real_chess) 
# Corner Harris Detection 
dst = cv2.cornerHarris(src=gray,blockSize=2,ksize=3,k=0.04) 
# result is dilated for marking the corners, not important to actual corner detection 
# this is toplot out the points on the image shown 
dst = cv2.dilate(dst,None) 
# Threshold for an optimal value, it may vary depending on the image. 
real_chess[dst>0.01*dst.max()]=[255,0,0] 
plt.imshow(real_chess) 
# more corners on blk pieces r detected  
# all the major corners r detected 
# the outside corners r not detected still 

Shi-Tomsasi corner detector & good featurers to track paper

# goodFeatureToTrack Function Parameters # * image Input 8-bit or floating-point 32-bit, single-channel image. # * corners Output vector of detected corners. # * maxCorners Maximum number of corners to return. If there are more corners than are found,the strongest of them is returned. `maxCorners <= 0` implies that no limit on the maximum is set and all detected corners are returned. # * qualityLevel Parameter characterizing the minimal accepted quality of image corners. The parameter value is multiplied by the best corner quality measure, which is the minimal eigenvalue (see #cornerMinEigenVal ) or the Harris function response (see #cornerHarris ). The corners with the quality measure less than the product are rejected. For example, if the best corner has the quality measure = 1500, and the qualityLevel=0.01 , then all the corners with the quality measure less than 15 are rejected. # Need to reset the images since we drew on them flat_chess = cv2.imread('flat_chessboard.png') plt.imshow(flat_chess) plt.show() flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB) plt.imshow(flat_chess) plt.show() gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2GRAY) plt.imshow(gray_flat_chess) plt.show() corners = cv2.goodFeaturesToTrack(gray_flat_chess,5,0.01,10) # 5 is max num of corners to return # 0.01 is the quality lvel par # the par val is mult by the best quality measure # whi is the min eigen val # 10 corners = np.int0(corners) for i in corners: x,y = i.ravel() # ravel is flattening cv2.circle(flat_chess,(x,y),3,255,-1) plt.imshow(flat_chess) corners = cv2.goodFeaturesToTrack(gray_flat_chess,64,0.01,10) corners = np.int0(corners) for i in corners: x,y = i.ravel() cv2.circle(flat_chess,(x,y),3,255,-1) plt.imshow(flat_chess) # these r the best corners # try on real real_chess = cv2.imread('real_chessboard.jpg') real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB) gray_real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY) corners = cv2.goodFeaturesToTrack(gray_real_chess,60,0.01,10) corners = np.int0(corners) for i in corners: x,y = i.ravel() cv2.circle(real_chess,(x,y),3,255,-1) plt.imshow(real_chess) # sm of the main chess board corners r not detected corners = cv2.goodFeaturesToTrack(gray_real_chess,100,0.01,10) corners = np.int0(corners) for i in corners: x,y = i.ravel() cv2.circle(real_chess,(x,y),3,255,-1) plt.imshow(real_chess)
In [ ]:
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png')
flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB)
plt.imshow(flat_chess)
In [ ]:
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_flat_chess)
In [ ]:
corner = cv2.goodFeaturesToTrack(gray_flat_chess,5,0.01,10)
corner = np.int0(corner)
for i in corner:
    x,y = i.ravel()
    cv2.circle(real_chess,(x,y),3,255,-1)
    
plt.imshow(real_chess)

Detecting the 64 corners in chess borad

goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance[, corners[, mask[, blockSize[, useHarrisDetector[, k]]]]]) -> corners

In [ ]:
corners = cv2.goodFeaturesToTrack(gray_flat_chess,64,0.01,10)
print('corners shape is : ',corners.shape)
corners = np.int0(corners);
print('cornershape is : ',corners.shape)
In [ ]:
for i in corners:
    x,y = i.ravel()
    cv2.circle(flat_chess,(x,y),3,255,-1)
plt.imshow(flat_chess)

try on real chess borad

In [ ]:
real_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/real_chessboard.jpg',)
real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB)
gray_real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_real_chess)
In [ ]:
corners = cv2.goodFeaturesToTrack(gray_real_chess,60,0.9,10)
for i in corners:
    x,y = i.ravel()
    cv2.circle(real_chess,(x,y),3,255,-1)
plt.imshow(real_chess)
In [ ]:
corners = cv2.goodFeaturesToTrack(gray_real_chess,100,0.9,10)
for i in corners:
    x,y = i.ravel()
    cv2.circle(real_chess,(x,y),3,255,-1)
plt.imshow(real_chess)

Canny Edge Detection

In [ ]:
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy_face.jpg') 
plt.imshow(img) 
plt.show() 
 
In [ ]:
edges = cv2.Canny(image=img, threshold1=127, threshold2=127) 
plt.imshow(edges) 
plt.show() 
 

different thersold values

In [ ]:
edges = cv2.Canny(image=img, threshold1=0, threshold2=255) 
plt.imshow(edges) 
plt.show() 
 

Choosing Thresholds, formulat for choosing thresholds

# Calculate the median pixel value med_val = np.median(img) # Lower bound is either 0 or 70% of the median value, whicever is higher lower = int(max(0, 0.7* med_val)) # Upper bound is either 255 or 30% above the median value, whichever is lower upper = int(min(255,1.3 * med_val)) edges = cv2.Canny(image=img, threshold1=lower , threshold2=upper) plt.imshow(edges) # Sometimes it helps to blur the images first, so we don't pick up minor edges. blurred_img = cv2.blur(img,ksize=(5,5)) edges = cv2.Canny(image=blurred_img, threshold1=lower , threshold2=upper) plt.imshow(edges) print(lower) print(upper)
In [ ]:
med_val = np.median(img)
lower = int(max(0,0.7* med_val))
upper = int(min(255,1.3* med_val))
edges = cv2.Canny(image=img, threshold1=lower , threshold2=upper) 
plt.imshow(edges)

After blur the image then after Perform edge detection looks good

In [ ]:
blurred_img = cv2.blur(img,ksize=(5,5)) 
edges = cv2.Canny(image=blurred_img, threshold1=lower , threshold2=upper) 
plt.imshow(edges)  
print(lower) 
print(upper) 

Grid detection

# # Grid Detection flat_chess = cv2.imread('flat_chessboard.png') plt.imshow(flat_chess,cmap='gray') # this works only for chessboard like grids found, corners = cv2.findChessboardCorners(flat_chess,(7,7)) # 7,7 as it is 8 x 8 it ds not find last one. if found: print('OpenCV was able to find the corners') else: print("OpenCV did not find corners. Double check your patternSize.") corners corners.shape flat_chess_copy = flat_chess.copy() cv2.drawChessboardCorners(flat_chess_copy, (7, 7), corners, found) plt.imshow(flat_chess_copy) # it marks the corners, and then marks the rows in raibow colors
In [ ]:
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png')
plt.imshow(flat_chess,cmap='gray')
In [ ]:
# open cv built in chessboard
found, corners = cv2.findChessboardCorners(flat_chess,(7,7))
if found: 
    print('OpenCV was able to find the corners') 
else: 
    print("OpenCV did not find corners. Double check your patternSize.") 
corners 
corners.shape 
flat_chess_copy = flat_chess.copy() 
cv2.drawChessboardCorners(flat_chess_copy, (7, 7), corners, found) 
plt.imshow(flat_chess_copy)
# # Circle Based Grids # this is another grid like pattern dots = cv2.imread('dot_grid.png') plt.imshow(dots) found, corners = cv2.findCirclesGrid(dots, (10,10), cv2.CALIB_CB_SYMMETRIC_GRID) found dbg_image_circles = dots.copy() cv2.drawChessboardCorners(dbg_image_circles, (10, 10), corners, found) plt.imshow(dbg_image_circles)

findCirclesGrid(image, patternSize, flags, blobDetector, parameters[, centers]) -> retval, centers . @brief Finds centers in the grid of c

In [ ]:
dots = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/dot_grid.png')
plt.imshow(dots)
found,corners = cv2.findCirclesGrid(dots,(10,10),corners,found)
# # Contour Detection # ## External vs Internal Contours img = cv2.imread('internal_external.png',0) img.shape plt.imshow(img,cmap='gray') # **findContours** # function will return back contours in an image, and based on the RETR method called, you can get back external, internal, or both: # * cv2.RETR_EXTERNAL:Only extracts external contours # * cv2.RETR_CCOMP: Extracts both internal and external contours organized in a two-level hierarchy # * cv2.RETR_TREE: Extracts both internal and external contours organized in a tree graph # * cv2.RETR_LIST: Extracts all contours without any internal/external relationship #image, contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) type(contours) print(len(contours)) # 22, if v count the shapes thy will b 22 print(type(hierarchy)) print(hierarchy.shape) hierarchy # -1s r ext contrs # 0s r eyes smiley face # 4s r pepprni slices # Draw External Contours # Set up empty array #external_contours = np.zeros(image.shape) external_contours = np.zeros(img.shape) # external contours r triangle, circle face or white circle of shape cutout external_contours.shape # 652,1080 is same as orignal img # this is pure black image img.shape plt.imshow(img) list(range(len(contours))) # For every entry in contours for i in range(len(contours)): # last column in the array is -1 if an external contour (no contours inside of it) if hierarchy[0][i][3] == -1: # v r checking if the last cell val == -1 # then it is ext otherwise it is int contour # We can now draw the external contours from the list of contours cv2.drawContours(external_contours, contours, i, 255, -1) # 255 is for white plt.imshow(external_contours,cmap='gray') # ext contours whi r touching the background so they r ext contours # int contours r touching the fg which r the eyes and other shapes inside # Create empty array to hold internal contours # internal contours r the eyes & smiley & slices of pepperoni image_internal = np.zeros(img.shape) # Iterate through list of contour arrays for i in range(len(contours)): # If third column value is NOT equal to -1 than its internal if hierarchy[0][i][3] != -1: # Draw the Contour cv2.drawContours(image_internal, contours, i, 255, -1) plt.imshow(image_internal,cmap='gray') # slices image_internal = np.zeros(img.shape) for i in range(len(contours)): if hierarchy[0][i][3] == 4: cv2.drawContours(image_internal, contours, i, 255, -1) plt.imshow(image_internal,cmap='gray') image_internal = np.zeros(img.shape) for i in range(len(contours)): if hierarchy[0][i][3] == 0: cv2.drawContours(image_internal, contours, i, 255, -1) plt.imshow(image_internal,cmap='gray') # # Feature Matching, Real world CV starts here def display(img,cmap='gray'): fig = plt.figure(figsize=(12,10)) ax = fig.add_subplot(111) ax.imshow(img,cmap='gray') reeses = cv2.imread('reeses_puffs.png') display(reeses) reeses = cv2.imread('reeses_puffs.png',0) display(reeses) # this is not an exact photo but a print out of it # v dont need an exact match photo or template here which is an adv in real world # this is a real pic of a cereals isle with diff cereals # none of them r exactly front facing like the abv img cereals = cv2.imread('many_cereals.jpg') display(cereals) # this is the target img cereals = cv2.imread('many_cereals.jpg',0) display(cereals) # at the btm left corner is the reesespuffs but it is not a family size # like the orgnl img # there r sm of the other boxes whi hv the family size on em # this may affect our feature matching as v hv family size on the top of our orgnl img

Contour Detection

In [ ]:
# ## External vs Internal Contours 
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/internal_external.png',0) 
img.shape 
plt.imshow(img,cmap='gray') 

contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) 
type(contours) 
 
In [ ]:
print(len(contours)) 
 
print(type(hierarchy)) 
 
print(hierarchy.shape) 
 
hierarchy 

external_contours = np.zeros(img.shape) 
external_contours.shape # 652,1080 is same as orignal img 

img.shape 
 
plt.imshow(img) 
In [ ]:
 
list(range(len(contours))) 
 
# For every entry in contours 
for i in range(len(contours)): 
   
    if hierarchy[0][i][3] == -1: 
   
        cv2.drawContours(external_contours, contours, i, 255, -1) 
   
 
plt.imshow(external_contours,cmap='gray') 
In [ ]:
# internal contours r the eyes & smiley & slices of pepperoni 
image_internal = np.zeros(img.shape) 
 
# Iterate through list of contour arrays 
for i in range(len(contours)): 
    # If third column value is NOT equal to -1 than its internal 
    if hierarchy[0][i][3] != -1: 
         
        # Draw the Contour 
        cv2.drawContours(image_internal, contours, i, 255, -1) 
         
plt.imshow(image_internal,cmap='gray') 
In [ ]:
 
# slices 
image_internal = np.zeros(img.shape) 
for i in range(len(contours)): 
    if hierarchy[0][i][3] == 4: 
        cv2.drawContours(image_internal, contours, i, 255, -1) 
plt.imshow(image_internal,cmap='gray') 
 
 
In [ ]:
 
In [ ]:
image_internal = np.zeros(img.shape) 
for i in range(len(contours)): 
    if hierarchy[0][i][3] == 0: 
        cv2.drawContours(image_internal, contours, i, 255, -1) 
plt.imshow(image_internal,cmap='gray') 
In [ ]:
import cv2
from matplotlib import pyplot as plt     
def display(img,cmap='gray'): 
    fig = plt.figure(figsize=(12,10)) 
    ax = fig.add_subplot(111) 
    ax.imshow(img,cmap='gray') 

reeses = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/reeses_puffs.png')      
display(reeses) 
 
reeses = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/reeses_puffs.png',0)      
display(reeses) 



cereals = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/many_cereals.jpg')  
display(cereals) 
 
# this is the target img 
cereals = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/many_cereals.jpg',0)  
display(cereals) 
# # this is a Brute Force Detection with ORB Descriptors # Initiate ORB detector orb = cv2.ORB_create() # this creates an obj of orb detector # and detects features # find the keypoints and descriptors with ORB # this is for computing features kp1, des1 = orb.detectAndCompute(reeses,None) # reeses is our orgnl img that v r searching for and None for masking # v r not masking anything here # kp is the keyopint & des1 is the descriptor # this is for cereals kp2, des2 = orb.detectAndCompute(cereals,None) # create BFMatcher object # stands for brute force matcher bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # the abv 2 r default pars # Match descriptors. matches = bf.match(des1,des2) # these r the matches sngl_mtch = matches[0] sngl_mtch.distance # gvs the dstnc, the less dstnc the btr the match and vice versa # Sort them in the order of their distance. matches = sorted(matches, key = lambda x:x.distance) matches # a grp of match objs # the less the distance the more closer and viceversa len(matches) # there r 265 matches reeses_matches = cv2.drawMatches(reeses,kp1,cereals,kp2,matches[:25],None,flags=2) display(reeses_matches) # Draw first 25 matches. # none of them r v good, this techniq ds not work unlike the target img # looks like the orgnl img # ******** From below in testing issues with sift creator *** in dev works in prodn # # Brute-Force Matching with SIFT Descriptors and Ratio Test # Create SIFT Object # scale in variant feature transform, it helps when img sizes r in dfrnt scale # our orgnl img is much larger than the cereals in the target img sift = cv2.xfeatures2d.SIFT_create() # find the keypoints and descriptors with SIFT kp1, des1 = sift.detectAndCompute(reeses,None) kp2, des2 = sift.detectAndCompute(cereals,None) # run upto here separately # if v get err for the abv # error: OpenCV(3.4.3) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src # \sift.cpp:1207: error: (-213:The function/feature is not implemented) # This algorithm is patented and is excluded in this configuration; # Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function # 'cv::xfeatures2d::SIFT::create # run the below one in cmd prompt as admin # C:\PrgramData\Anaconda3 first try this #!conda install -c menpo opencv if it ds not work try this open Ana prompt as Admin # pip3 uninstall opencv-contrib-python # pip install opencv-contrib-python

this is a Brute Force Detection with ORB Descriptors

with helpl of below version only detection will work due pentent version

  • pip install opencv-python==3.4.2.16
  • pip install opencv-contrib-python==3.4.2.16
In [ ]:
orb = cv2.ORB_create() 
kp1, des1 = orb.detectAndCompute(reeses,None) 
kp2, des2 = orb.detectAndCompute(cereals,None) 

 
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 
matches = bf.match(des1,des2) 
 
sngl_mtch = matches[0] 
sngl_mtch.distance 
matches = sorted(matches, key = lambda x:x.distance) 
matches  
 
len(matches) 

reeses_matches = cv2.drawMatches(reeses,kp1,cereals,kp2,matches[:25],None,flags=2) 
display(reeses_matches) 
sift = cv2.xfeatures2d.SIFT_create() 
 
# find the keypoints and descriptors with SIFT 
kp1, des1 = sift.detectAndCompute(reeses,None) 
kp2, des2 = sift.detectAndCompute(cereals,None) 
# first try this 
#!conda install -c menpo opencv 
# if it ds not work try this 
# open Ana prompt as Admin 
# pip3 uninstall opencv-contrib-python 
# pip install opencv-contrib-python 
 
 

BFMatcher with default params

In [ ]:
bf = cv2.BFMatcher() 
matches = bf.knnMatch(des1,des2, k=2) 
matches 
# first match is btr than a 2nd match and so on 
# first col is the first best mtch 
# sec col is the 2nd best mtch 
# if first mtch is close in dstnc to the 2nd mtch then  
# overal it is a good feature to mtch on  
# also if v hv a strong mtch in the first col 
# nd 2nd bst mtch is far away in dstnc 
# then this descriptor whi is in the first row 
 
# Apply ratio test 
# is used for checking if the 2 mtches r close in dist or not 
good = [] 
for match1,match2 in matches: 
    if match1.distance < 0.75*match2.distance: 
  # if m1 dist is lt 75% of mtch2 dist  
  # then the descriptor is a gd mtch 
        good.append([match1]) 
  # mtch1 is a ratio of 75% of mtch2 v call it a ratio test 
  # less dist == btr mtch 
print(good) 
 
print(len(good)) 
# 78 r best mtches 
 
print(matches) 
In [ ]:
import cv2
# cv2.drawMatchesKnn expects list of lists as matches. 
sift_matches = cv2.drawMatchesKnn(reeses,kp1,cereals,kp2,good,None,flags=2) 
display(sift_matches) 
# # FLANN based Matcher 
# Initiate SIFT detector 
sift = cv2.xfeatures2d.SIFT_create() 
 
# find the keypoints and descriptors with SIFT 
kp1, des1 = sift.detectAndCompute(reeses,None) 
kp2, des2 = sift.detectAndCompute(cereals,None) 
 
# FLANN parameters 
FLANN_INDEX_KDTREE = 0 
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) 
search_params = dict(checks=50)   
 
flann = cv2.FlannBasedMatcher(index_params,search_params) 
 
matches = flann.knnMatch(des1,des2,k=2) 
 
good = [] 

 
# ratio test 
for i,(match1,match2) in enumerate(matches): 
    if match1.distance < 0.7*match2.distance: 
         
        good.append([match1]) 
 
 
flann_matches = cv2.drawMatchesKnn(reeses,kp1,cereals,kp2,good,None,flags=0) 
display(flann_matches) 
 
 
In [ ]:
# Initiate SIFT detector 
sift = cv2.xfeatures2d.SIFT_create() 
 
# find the keypoints and descriptors with SIFT 
kp1, des1 = sift.detectAndCompute(reeses,None) 
kp2, des2 = sift.detectAndCompute(cereals,None) 
 
# FLANN parameters 
FLANN_INDEX_KDTREE = 0 
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) 
search_params = dict(checks=50)   
 
flann = cv2.FlannBasedMatcher(index_params,search_params) 
 
matches = flann.knnMatch(des1,des2,k=2) 
 
# Need to draw only good matches, so create a mask 
matchesMask = [[0,0] for i in range(len(matches))] 
 
# ratio test 
for i,(match1,match2) in enumerate(matches): 
    if match1.distance < 0.7*match2.distance: 
        matchesMask[i]=[1,0] 
draw_params = dict(matchColor = (0,255,0), 
                   singlePointColor = (255,0,0), 
                   matchesMask = matchesMask, 
                   flags = 0) 
 
flann_matches = cv2.drawMatchesKnn(reeses,kp1,cereals,kp2,matches,None,**draw_params) 
 
display(flann_matches) 
# *** Watershed Algorithm *** # import numpy as np import cv2 import matplotlib.pyplot as plt get_ipython().magic('matplotlib inline') def display(img,cmap=None): fig = plt.figure(figsize=(10,8)) ax = fig.add_subplot(111) ax.imshow(img,cmap=cmap) # ## Task: Draw Contours Around the Coins # ## Common Coin Example # ## Naive Approach # simply use a threshold and then use findContours. sep_coins = cv2.imread('pennies.jpg') display(sep_coins) # for humans it is ez to tell thy r 6 sep coins but for a comp it may think # f it as 1 giant img # our task is to segment these into 7 dfrnt segments # 6 diff coins and 1 bg

Watershed Algorithm

In [ ]:
 
import numpy as np 
import cv2 
import matplotlib.pyplot as plt 
get_ipython().magic('matplotlib inline') 
def display(img,cmap=None): 
    fig = plt.figure(figsize=(10,8)) 
    ax = fig.add_subplot(111) 
    ax.imshow(img,cmap=cmap) 
    
sep_coins = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/pennies.jpg') 
display(sep_coins) 
 

Apply Median Blurring

# too much detail in this image, including light, the face edges on the coins, # and too much detail in the background. use Median Blur Filtering to blur the image a bit, # which will be useful later on when we threshold. sep_blur = cv2.medianBlur(sep_coins,25) display(sep_blur) # 25 is the kernel size # the img is 4000 x 3000 pxls # convert this to grayscale gray_sep_coins = cv2.cvtColor(sep_blur,cv2.COLOR_BGR2GRAY) display(gray_sep_coins,cmap='gray') # ## Binary Threshold for seprting fg & bg ret, sep_thresh = cv2.threshold(gray_sep_coins,160,255,cv2.THRESH_BINARY_INV) display(sep_thresh,cmap='gray') # the inversion is for inverting black & white as w & b ret, sep_thresh = cv2.threshold(gray_sep_coins,127,255,cv2.THRESH_BINARY_INV) display(sep_thresh,cmap='gray') # ntc as v lower it there is distortion due to the faces on the coins ret, sep_thresh = cv2.threshold(gray_sep_coins,160,255,cv2.THRESH_BINARY_INV) display(sep_thresh,cmap='gray')
In [ ]:
sep_blur = cv2.medianBlur(sep_coins,25) 
display(sep_blur) 
In [ ]:
gray_sep_coins = cv2.cvtColor(sep_blur,cv2.COLOR_BGR2GRAY) 
display(gray_sep_coins,cmap='gray') 
 
In [ ]:
ret, sep_thresh = cv2.threshold(gray_sep_coins,160,255,cv2.THRESH_BINARY_INV) 
display(sep_thresh,cmap='gray') 
In [ ]:
ret, sep_thresh = cv2.threshold(gray_sep_coins,127,255,cv2.THRESH_BINARY_INV) 
display(sep_thresh,cmap='gray') 
In [ ]:
 
ret, sep_thresh = cv2.threshold(gray_sep_coins,160,255,cv2.THRESH_BINARY_INV) 
display(sep_thresh,cmap='gray') 

FindContours

#image, contours, hierarchy = cv2.findContours(sep_thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) contours, hierarchy = cv2.findContours(sep_thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) # For every entry in contours for i in range(len(contours)): # last column in the array is -1 if an external contour (no contours inside of it) if hierarchy[0][i][3] == -1: # this is an ext contour # We can draw the external contours from the list of contours cv2.drawContours(sep_coins, contours, i, (255, 0, 0), 10) display(sep_coins) # nte this is 1 giant contour and the gaps r not compltely clsd # this is a problem # so bring in the WS alg
In [ ]:
contours, hierarchy = cv2.findContours(sep_thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) 
for i in range(len(contours)): 
    if hierarchy[0][i][3] == -1:   
        cv2.drawContours(sep_coins, contours, i, (255, 0, 0), 10) 
display(sep_coins) 

Watershed Algorithm

# the watershed algorithm apporach to draw contours around the pennies. # ## Using the WaterShed Algorithm # #### Step 1: Read Image img = cv2.imread('pennies.jpg') # #### Step 2: Apply Blur img = cv2.medianBlur(img,35) # v r using huge kernel size display(img) # #### Step 3: Convert to Grayscale gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV) display(thresh,cmap='gray') # nt the noise/distortions whi v dont need at this stg # #### Step 4: use OTSUs methd of thresholding, Apply Threshold (Inverse Binary with OTSU ) ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) display(thresh,cmap='gray') # ntc they r still connected and v hv not yet achieved the separation
In [ ]:
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/pennies.jpg') 
img = cv2.medianBlur(img,35) # v r using huge kernel size 
display(img) 
In [ ]:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV) 
display(thresh,cmap='gray') 
In [ ]:
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 
display(thresh,cmap='gray') 

Optional Step 5: Noise Removal

# noise removal kernel = np.ones((3,3),np.uint8) kernel # create a kernel opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2) display(opening,cmap='gray') # no effect here but for discipline # this works if v use thresh_bin_inv instead of otsu v wld see the dfrncs # the fund prblm v hv is tht the coins r still cnnctd to ea othr # and it is treated as 1 big img and not sep coins inspite of all abv steps # wt v need to do for WS alg is set seeds that v r sure in the fg # v wnt 6 seeds one for ea of the cntr of the coins # so how to grab the things in the bg & fg # using dist transform # in a binry img v hv 0s and 1s or 0s and 255s # wht DT ds is as the pixls is away frm 0s the val gets higher # means they look higher # #### Step 6: Grab Background that you are sure of # sure background area sure_bg = cv2.dilate(opening,kernel,iterations=3) display(sure_bg,cmap='gray')
In [ ]:
kernel = np.ones((3,3),np.uint8) 
kernel

opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2) 
display(opening,cmap='gray') 
In [ ]:
sure_bg = cv2.dilate(opening,kernel,iterations=3) 
display(sure_bg,cmap='gray') 
In [ ]:
sure_bg = cv2.dilate(opening,kernel,iterations=4) 
display(sure_bg,cmap='gray') 

Step 7: Find Sure Foreground

# Finding sure foreground area dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5) display(dist_transform,cmap='gray') # v hv 6 clear pts in the fg # nxt apply thresholding to this to describe the dots or centers # then apply to WS alg whi will then und the 6 segments it will look into ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0) # 70% of max val in dist transform display(sure_fg,cmap='gray') # these 6 pts r absolutely in the fg coz v did a thresholding and then a DT display(dist_transform,cmap='gray')
In [ ]:
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5) 
display(dist_transform,cmap='gray') 
In [ ]:
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0) 
display(sure_fg,cmap='gray') 
In [ ]:
 
display(dist_transform,cmap='gray') 

Step 8: Find Unknown Region

# Finding unknown region is the reg whi is anything whi is in DT but not is sure_fg # thts the U R nd thts wht v want WS alg to find sure_fg = np.uint8(sure_fg) sure_fg unknown = cv2.subtract(sure_bg,sure_fg) # unk reg is remvng bg frm fg display(unknown,cmap='gray') # the whte reg is the Unknown Reg means v dont know if it blngs to fg or bg # nxt label mark the 6 pts as seeds and hv the WS alg use them to find the segments # #### Step 9: Label Markers of Sure Foreground # Marker labelling # feed in 6 pts as the markers ret, markers = cv2.connectedComponents(sure_fg) markers # all r 0s # Add one to all labels so that sure background is not 0, but 1 markers = markers+1 # this will mark the region of unknown with zero markers[unknown==255] = 0 display(markers,cmap='gray') # this is manually marking the labels # next automatic way of marking the labels # the black region in the mid is the unknown region # means it is not sure it is fg or bg # these markers will act as seeds to the WS alg
In [ ]:
sure_fg = np.uint8(sure_fg) 
sure_fg  
 
unknown = cv2.subtract(sure_bg,sure_fg) 
display(unknown,cmap='gray') 
In [ ]:
ret, markers = cv2.connectedComponents(sure_fg) 
markers 
In [ ]:
markers = markers+1 
markers[unknown==255] = 0 
display(markers,cmap='gray') 
In [ ]:
# #### next Step 10: Apply Watershed Algorithm to find Markers 
markers = cv2.watershed(img,markers) 
display(markers) 

Step 11: Find Contours on Markers

#image, contours, hierarchy = cv2.findContours(markers.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) contours, hierarchy = cv2.findContours(markers.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) # For every entry in contours for i in range(len(contours)): # last column in the array is -1 if an external contour (no contours inside of it) if hierarchy[0][i][3] == -1: # We can now draw the external contours from the list of contours cv2.drawContours(sep_coins, contours, i, (255, 0, 0), 10) display(sep_coins) # this will segment the coins seprtly
In [ ]:
contours, hierarchy = cv2.findContours(markers.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) 
for i in range(len(contours)): 
    if hierarchy[0][i][3] == -1: 
        cv2.drawContours(sep_coins, contours, i, (255, 0, 0), 10) 
display(sep_coins) 

Custom Seeds with the WaterShed Algorithm

# Previously we did a lot of work for OpenCV to set Markers to provide seeds to the # Watershed Algorithm. but v can auto do this # ### Read in the Image and Make a Copy road = cv2.imread('road_image.jpg') road_copy = np.copy(road) plt.imshow(road) # #### Create an empty space for the results to be drawn print(road.shape) print(road.shape[:2]) marker_image = np.zeros(road.shape[:2],dtype=np.int32) # this is for markers, this will tk only x & y plt.imshow(marker_image) segments = np.zeros(road.shape,dtype=np.uint8) # this is for segments segments.shape
In [ ]:
road = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/road_image.jpg') 
road_copy = np.copy(road) 
plt.imshow(road) 

 
print(road.shape) 
 
print(road.shape[:2]) 
In [ ]:
 
marker_image = np.zeros(road.shape[:2],dtype=np.int32) 
plt.imshow(marker_image) 
 
segments = np.zeros(road.shape,dtype=np.uint8) 
segments.shape 
 
 

Create colors for Markers

from matplotlib import cm # Returns (R,G,B,Alpha) we only need RGB values cm.tab10(0) # these r like templates of colors # they r scaled btw 0 & 1 cm.tab10(1) np.array(cm.tab10(0)) # cnvrt to an np arr np.array(cm.tab10(0))[:3] # gvs us r g b np.array(cm.tab10(0))[:3]*255 # mult ply by 255 x = np.array(cm.tab10(0))[:3]*255 tuple(x.astype(int)) # cnvrt to a tuple # a function for all those steps def create_rgb(i): x = np.array(cm.tab10(i))[:3]*255 return tuple(x) colors = [] # One color for each single digit for i in range(10): colors.append(create_rgb(i)) colors # ea tuple is a uniq mapping of r g b
In [ ]:
from matplotlib import cm 
cm.tab10(0)
cm.tab10(1) 
 
np.array(cm.tab10(0))  
 
np.array(cm.tab10(0))[:3] 
 
np.array(cm.tab10(0))[:3]*255 
 
x = np.array(cm.tab10(0))[:3]*255 
 
tuple(x.astype(int)) 

def create_rgb(i): 
    x = np.array(cm.tab10(i))[:3]*255 
    return tuple(x) 
 
colors = [] 

for i in range(10): 
    colors.append(create_rgb(i)) 
colors 

Setting Up Callback Function

# Numbers 0-9 n_markers = 10 # Default settings current_marker = 1 marks_updated = False def mouse_callback(event, x, y, flags, param): global marks_updated if event == cv2.EVENT_LBUTTONDOWN: # TRACKING FOR MARKERS # Markers pasd to the WS algo cv2.circle(marker_image, (x, y), 10, (current_marker), -1) # DISPLAY ON USER IMAGE # User sees on the img cv2.circle(road_copy, (x, y), 10, colors[current_marker], -1) marks_updated = True cv2.namedWindow('Road Image') cv2.setMouseCallback('Road Image', mouse_callback) while True: # SHow the 2 windows cv2.imshow('WaterShed Segments', segments) # this is the black one with segments hidden cv2.imshow('Road Image', road_copy) # this gets updated when clkd # Close everything if Esc is pressed k = cv2.waitKey(1) if k == 27: break # Clear all colors and start over if 'c' is pressed elif k == ord('c'): road_copy = road.copy() marker_image = np.zeros(road.shape[0:2], dtype=np.int32) segments = np.zeros(road.shape,dtype=np.uint8) # If a number 0-9 is chosen index the color elif k > 0 and chr(k).isdigit(): # chr converts to printable digit current_marker = int(chr(k)) # CODE TO CHECK INCASE USER IS CARELESS '''n = int(chr(k)) if 1 <= n <= n_markers: current_marker = n''' # If we clicked somewhere, call the watershed algorithm on our chosen markers if marks_updated: marker_image_copy = marker_image.copy() cv2.watershed(road, marker_image_copy) segments = np.zeros(road.shape,dtype=np.uint8) for color_ind in range(n_markers): segments[marker_image_copy == (color_ind)] = colors[color_ind] #marks_updated = False cv2.destroyAllWindows()

Need to press number from 1-10, for each mouse click

In [ ]:
n_markers = 10 
current_marker = 1 
marks_updated = False 
def mouse_callback(event, x, y, flags, param): 
    global marks_updated  
    if event == cv2.EVENT_LBUTTONDOWN: 
        cv2.circle(marker_image, (x, y), 10, (current_marker), -1) 
 
        cv2.circle(road_copy, (x, y), 10, colors[current_marker], -1) 
        marks_updated = True 

cv2.namedWindow('Road Image') 
cv2.setMouseCallback('Road Image', mouse_callback) 
 
while True: 
    cv2.imshow('WaterShed Segments', segments)  
    cv2.imshow('Road Image', road_copy)  
    k = cv2.waitKey(1) 
    if k == 27: 
        break 
 
    elif k == ord('c'): 
        road_copy = road.copy() 
        marker_image = np.zeros(road.shape[0:2], dtype=np.int32) 
        segments = np.zeros(road.shape,dtype=np.uint8) 
    elif k > 0 and chr(k).isdigit(): 
        current_marker  = int(chr(k)) 

    if marks_updated: 
        marker_image_copy = marker_image.copy() 
        cv2.watershed(road, marker_image_copy) 
         
        segments = np.zeros(road.shape,dtype=np.uint8) 
         
    for color_ind in range(n_markers): 
        segments[marker_image_copy == (color_ind)] = colors[color_ind] 
cv2.destroyAllWindows() 
 
 

Part 3

# # Face Detection with Haar Cascades # # **This is face *detection* NOT face *recognition*. # We are only detecting if a face is in an image, # not who the face actually is. # That requires deep learning ** import numpy as np import cv2 import matplotlib.pyplot as plt get_ipython().magic('matplotlib inline') # ## Images nadia = cv2.imread('Nadia_Murad.jpg',0) denis = cv2.imread('Denis_Mukwege.jpg',0) solvay = cv2.imread('solvay_conference.jpg',0) plt.imshow(nadia,cmap='gray') plt.show() plt.imshow(denis,cmap='gray') plt.show() plt.imshow(solvay,cmap='gray') plt.show()
In [ ]:
 
import numpy as np 
import cv2  
import matplotlib.pyplot as plt
# %matplotlib inline # both line interchangeble
get_ipython().magic('matplotlib inline') 

nadia = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/Nadia_Murad.jpg',0) 
denis = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/Denis_Mukwege.jpg',0) 
solvay = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/solvay_conference.jpg',0) 
 
plt.imshow(nadia,cmap='gray') 
plt.show() 
In [ ]:
plt.imshow(denis,cmap='gray') 
plt.show() 
In [ ]:
plt.imshow(solvay,cmap='gray') 
plt.show() 
 
 

Cascade Files

# OpenCV comes with these pre-trained cascade files, # ve located the .xml files for you in our own DATA folder. # ## Face Detection face_cascade = cv2.CascadeClassifier('haarcascades\\haarcascade_frontalface_default.xml') # this is a list of 6000 classifiers or features tht r going to b # pssd thru the img to see if it fits and indicate if a face is there def detect_face(img): face_img = img.copy() face_rects = face_cascade.detectMultiScale(face_img) # face_rects r a grp of x & y positions and wid & hts of rects for (x,y,w,h) in face_rects: cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10) #255 s r for white img #10 is thickness return face_img result = detect_face(denis) plt.imshow(result,cmap='gray') plt.show() result = detect_face(nadia) plt.imshow(result,cmap='gray') plt.show() # Gets errors! result = detect_face(solvay) plt.imshow(result,cmap='gray') plt.show() # mult faces, sm of em r not looking at camera # ntc the dbl face def adj_detect_face(img): face_img = img.copy() face_rects = face_cascade.detectMultiScale(face_img,scaleFactor=1.2, minNeighbors=5) # scaleFac is specifying how much the img size is reduced # how many neighbors ea rect must hv for (x,y,w,h) in face_rects: cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10) return face_img # Doesn't detect the side face. result = adj_detect_face(solvay) plt.imshow(result,cmap='gray') plt.show() # ntc the side face is not detected # this is tradeoff btwn abv one & this one # play with scalefac & min neighbors
In [ ]:
face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml') 

def detect_face(img): 
    face_img = img.copy() 
    face_rects = face_cascade.detectMultiScale(face_img)  
    for (x,y,w,h) in face_rects:  
        cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10)  
    return face_img 
 
result = detect_face(denis) 

 
plt.imshow(result,cmap='gray') 
plt.show() 
In [ ]:
result = detect_face(nadia) 
plt.imshow(result,cmap='gray') 
plt.show() 
In [ ]:
result = detect_face(solvay) 
plt.imshow(result,cmap='gray') 
plt.show() 
In [ ]:
def adj_detect_face(img): 
    face_img = img.copy() 
    face_rects = face_cascade.detectMultiScale(face_img,scaleFactor=1.2, minNeighbors=5)  
    for (x,y,w,h) in face_rects:  
        cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10)  
    return face_img 
     
result = adj_detect_face(solvay) 
plt.imshow(result,cmap='gray') 
plt.show() 

Eye Cascade File

eye_cascade = cv2.CascadeClassifier('haarcascades\\haarcascade_eye.xml') def detect_eyes(img): face_img = img.copy() #eyes = eye_cascade.detectMultiScale(face_img) eyes = eye_cascade.detectMultiScale(face_img,scaleFactor=1.2, minNeighbors=5) for (x,y,w,h) in eyes: cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10) return face_img result = detect_eyes(nadia) plt.imshow(result,cmap='gray') # play with scalefac & min neighbors eyes = eye_cascade.detectMultiScale(denis) # White around the pupils is not distinct enough to detect Denis' # eyes meanqs white in the eyes is not white but dark # they r same color as skin, this may bcoz of photo editing done # by the photographer, but for nadia white is white # and that is one of the main features or cascade is looking for # so the solution is find an un edited photo of denis result = detect_eyes(denis) plt.imshow(result,cmap='gray')
In [ ]:
eye_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_eye.xml') 

def detect_eyes(img): 
    face_img = img.copy() 
    eyes = eye_cascade.detectMultiScale(face_img,scaleFactor=1.2, minNeighbors=5)  
    for (x,y,w,h) in eyes:  
        cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10)  
    return face_img 
 
result = detect_eyes(nadia) 
plt.imshow(result,cmap='gray') 

eyes = eye_cascade.detectMultiScale(denis)  

result = detect_eyes(denis) 
plt.imshow(result,cmap='gray') 

Conjunction with Video

In [ ]:
cap = cv2.VideoCapture(0)  
while True:  
    ret, frame = cap.read(0)  
    frame = detect_face(frame) # detect_face() function wrote in previos 
    cv2.imshow('Video Face Detection', frame)  
    c = cv2.waitKey(1)  
    if c == 27:  
        break  
         
cap.release()  
cv2.destroyAllWindows() 
 
 

Detect Face & Eyes & Smile

import cv2 # Loading the cascades face_cascade = cv2.CascadeClassifier('chaarcascades\\haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascades\\haarcascade_eye.xml') smile_cascade = cv2.CascadeClassifier('haarcascades\\haarcascade_smile2.xml') # Defining a function that will do the detections def detect(gray, frame): faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) roi_gray = gray[y:y+h, x:x+w] roi_color = frame[y:y+h, x:x+w] eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 22) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2) smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 22) for (sx, sy, sw, sh) in smiles: cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2) return frame # Doing some Face Recognition with the webcam video_capture = cv2.VideoCapture(0) while True: _, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) canvas = detect(gray, frame) cv2.imshow('Video', canvas) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
In [ ]:
import cv2 

face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml') 
eye_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_eye.xml') 
smile_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_smile2.xml') 

def detect(gray, frame): 
    faces = face_cascade.detectMultiScale(gray, 1.3, 5) 
    for (x, y, w, h) in faces: 
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) 
        roi_gray = gray[y:y+h, x:x+w] 
        roi_color = frame[y:y+h, x:x+w] 
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 22) 
        for (ex, ey, ew, eh) in eyes: 
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2) 
        smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 22) 
        for (sx, sy, sw, sh) in smiles: 
            cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2) 
    return frame 

video_capture = cv2.VideoCapture(0) 
while True: 
    _, frame = video_capture.read() 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    canvas = detect(gray, frame) 
    cv2.imshow('Video', canvas) 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break 
video_capture.release() 
cv2.destroyAllWindows() 
 

Cartoon & Edge effect

In [ ]:
import matplotlib.image as mpimg 
import matplotlib.pyplot as plt 
import numpy as np 
import cv2 
 
def Cartoon(image_color): 
    output_image = cv2.stylization(image_color, sigma_s=100, sigma_r=0.3) 
    return output_image 
 
 
def LiveCamEdgeDetection_canny(image_color): 
    threshold_1 = 30 
    threshold_2 = 80 
    image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY) 
    canny = cv2.Canny(image_gray, threshold_1, threshold_2) 
    return canny 
 
 
cap = cv2.VideoCapture(0) 
 
while True: 
    ret, frame = cap.read() # Cap.read() returns a ret bool to indicate success. 
    cv2.imshow('Live Edge Detection', Cartoon(frame)) 
     
    #cv2.imshow('Live Edge Detection', LiveCamEdgeDetection_canny(frame)) 
    cv2.imshow('Webcam Video', frame) 
    if cv2.waitKey(1) == 13: #13 Enter Key 
        break 
         
cap.release() # camera release  
cv2.destroyAllWindows()   
 
 

Russian License Plate Blurring

# # object detection our goal will be to use Haar Cascades to blur license # plates detected in an image! # Russians are famous for having some of the most entertaining # DashCam footage on the internet Google Search "Russian DashCam". # a lot of the footage contains license plates, # help and create a license plat blurring tool? # OpenCV comes with a Russian license plate detector .xml file # we can use like we used the face detection files # ( it does not come with license detectors for other countries!) # Import the usual libraries you think you'll need.** import cv2 import numpy as np import matplotlib.pyplot as plt get_ipython().magic('matplotlib inline') # Read in the car_plate.jpg file from the DATA folder.** img = cv2.imread('car_plate.jpg') # Create a function that displays the image in a larger scale and correct coloring for matplotlib.** def display(img): fig = plt.figure(figsize=(10,8)) ax = fig.add_subplot(111) new_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) ax.imshow(new_img) display(img)
In [ ]:
import cv2 
import numpy as np 
import matplotlib.pyplot as plt 
get_ipython().magic('matplotlib inline') 
 
img = cv2.imread('c:/Users/hari/Desktop/datasets/Naresh_datasets/car_plate.jpg') 
 
def display(img): 
    fig = plt.figure(figsize=(10,8)) 
    ax = fig.add_subplot(111) 
    new_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
    ax.imshow(new_img) 
display(img) 

Load the haarcascade_russian_plate_number.xml file.**

In [ ]:
plate_cascade = cv2.CascadeClassifier('c:/Users/hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_russian_plate_number.xml') 
 
def detect_plate(img): 
    plate_img = img.copy() 
    plate_rects = plate_cascade.detectMultiScale(plate_img,scaleFactor=1.3, minNeighbors=3)  
    for (x,y,w,h) in plate_rects:  
        cv2.rectangle(plate_img, (x,y), (x+w,y+h), (0,0,255), 4)  
    return plate_img 
 
result = detect_plate(img) 
display(result) 

FINAL Edit the function so that is effectively blurs the detected plate, instead of just drawing a rectangle around it. the steps:

# 1. The hardest part is converting the (x,y,w,h) information into the # dimension values v need to grab an ROI # (just need to convert the information about the top left corner of the # rectangle and width and height, into indexing position values. # 2. Once you've grabbed the ROI using the (x,y,w,h) values returned, # v blur that ROI. use cv2.medianBlur for this. # 3. Now that v have a blurred version of the ROI (the license plate) # v will want to paste this blurred image back on to the original image # at the same original location. Simply using Numpy indexing and slicing to # reassign that area of the original image to the blurred roi. def detect_and_blur_plate(img): plate_img = img.copy() roi = img.copy() plate_rects = plate_cascade.detectMultiScale(plate_img,scaleFactor=1.3, minNeighbors=3) for (x,y,w,h) in plate_rects: roi = roi[y:y+h,x:x+w] #specify roi blurred_roi = cv2.medianBlur(roi,7) # blur it plate_img[y:y+h,x:x+w] = blurred_roi # replace the roi with blurd img return plate_img result = detect_and_blur_plate(img) display(result)
In [ ]:
def detect_and_blur_plate(img): 
    plate_img = img.copy() 
    roi = img.copy() 
    plate_rects = plate_cascade.detectMultiScale(plate_img,scaleFactor=1.3, minNeighbors=3)  
    for (x,y,w,h) in plate_rects:  
        roi = roi[y:y+h,x:x+w] 
        blurred_roi = cv2.medianBlur(roi,7) 
        plate_img[y:y+h,x:x+w] = blurred_roi 
    return plate_img 
     
result = detect_and_blur_plate(img) 
display(result) 
 
 

Object Tracking

# # Optical Flow # restart the kernel if v ever run these cells, # as the tracking algo can get caught in a loop with camera. # ## Lucas-Kanade Optical Flow import numpy as np import cv2 # Parameters for ShiTomasi corner detection (good features to track paper) # these r corner tracking pars corner_track_params = dict(maxCorners = 10, qualityLevel = 0.3, minDistance = 7, blockSize = 7 ) # detect 10 corners on first frame and track m # ### Parameters for Lucas Kanade Optical Flow Detect the motion of specific points or the aggregated motion of regions by modifying the winSize argument. This determines the integration window size. Small windows are more sensitive to noise and miss larger motions. Large windows will “survive” an occlusion. # The integration appears smoother with the larger window size. # criteria has two here - the max number (10 above) of iterations and epsilon # (0.03 above). More iterations means a more exhaustive search, and a smaller epsilon # finishes earlier. These are useful in exchanging speed vs accuracy, but mainly # stay the same. # When maxLevel is 0, it is the same algorithm without using pyramids # (ie, calcOpticalFlowLK). Pyramids allow finding optical flow at various resolutions # of the image. # Parameters for lucas kanade optical flow lk_params = dict( winSize = (200,200), maxLevel = 2, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10,0.03)) # eps is epsilon 0.03 # max num of iterations is 10 # more iters means more search in the crnt frame vs prev frame # a samll eps means finish earlier # v need to play with these vals # v r exchanging speed of tracking vs accuracy of tracking # these r lucas kanade pars # Capture the video cap = cv2.VideoCapture(0) # Grab the very first frame of the stream ret, prev_frame = cap.read() # read first frame and rename it as prev frame # Grab a grayscale image (We will refer to this as the previous frame) prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY) # Wht r the pts to track # Get the corners prevPts = cv2.goodFeaturesToTrack(prev_gray, mask = None, **corner_track_params) # Create a matching mask of the previous frame for drawing later mask = np.zeros_like(prev_frame) # For displaying the pts and tracking em # creates a np arr of same shape as prev_frame while True: # Grab current frame ret,frame = cap.read() # Grab gray scale frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Calculate the Optical Flow on the Gray Scale Frame nextPts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray, frame_gray, prevPts, None, **lk_params) # pyr stands for pyramid lucas kanade # 3 objs r returnd # v r passing in the prev frame/img, then the crnt frame/img, # then the prev pts # nd get the nextPts # Using the returned status array (the status output) # status output status vector (of unsigned chars); # each element of the vector is set to 1 if # the flow for the corresponding features has been found, # otherwise, it is set to 0. good_new = nextPts[status==1] good_prev = prevPts[status==1] # this is connecting the prev pts to the next pts # Use ravel to get points to draw lines and circles for i,(new,prev) in enumerate(zip(good_new,good_prev)): x_new,y_new = new.ravel() x_prev,y_prev = prev.ravel() # flatten # a cmplx np arr tracking 10 dfrnt pts or corners # draw lines using the mask created # from the prev/first frame to crnt frame # green color & 3 is thickness mask = cv2.line(mask, (x_new,y_new),(x_prev,y_prev), (0,255,0), 3) # draw line connecting prev pts to crnt pts # Draw red circles at corner points frame = cv2.circle(frame,(x_new,y_new),8,(0,0,255),-1) # Draw circles where the crnt frma is # Display the image along with the mask we drew the line on. img = cv2.add(frame,mask) # add frame of circles and masks of lines cv2.imshow('tracking',img) k = cv2.waitKey(30) & 0xff if k == 27: break # update the previous frame and previous points for the next iter prev_gray = frame_gray.copy() prevPts = good_new.reshape(-1,1,2) cv2.destroyAllWindows() cap.release()
In [ ]:
import numpy as np 
import cv2  
corner_track_params = dict(maxCorners = 10, 
                       qualityLevel = 0.3, 
                       minDistance = 7, 
                       blockSize = 7 ) 
lk_params = dict( winSize  = (200,200), 
                  maxLevel = 2, 
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10,0.03)) 
cap = cv2.VideoCapture(0) 
ret, prev_frame = cap.read() 
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY) 
 
prevPts = cv2.goodFeaturesToTrack(prev_gray, mask = None, **corner_track_params) 
mask = np.zeros_like(prev_frame) 
while True: 
    ret,frame = cap.read() 
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    nextPts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray, frame_gray, prevPts, None, **lk_params) 
    good_new = nextPts[status==1] 
    good_prev = prevPts[status==1] 
    for i,(new,prev) in enumerate(zip(good_new,good_prev)): 
        x_new,y_new = new.ravel() 
        x_prev,y_prev = prev.ravel() 
        mask = cv2.line(mask, (x_new,y_new),(x_prev,y_prev), (0,255,0), 3) 
        frame = cv2.circle(frame,(x_new,y_new),8,(0,0,255),-1) 
    img = cv2.add(frame,mask) 
    cv2.imshow('tracking',img) 
    k = cv2.waitKey(30) & 0xff 
    if k == 27: 
        break 
    prev_gray = frame_gray.copy() 
    prevPts = good_new.reshape(-1,1,2) 

cv2.destroyAllWindows() 
cap.release() 

Dense Optical Flow in OpenCV

# calcOpticalFlowFarneback(prev, next, flow, pyr_scale, levels, winsize, iterations, # poly_n, poly_sigma, flags) -> flow # This function computes a dense optical flow using the Gunnar Farneback's algorithm. # parameters for the function : # * prev first 8-bit single-channel input image. # * next second input image of the same size and the same type as prev. # * flow computed flow image that has the same size as prev and type CV_32FC2. # * pyr_scale parameter, specifying the image scale (\<1) to build pyramids for each image # * pyr_scale=0.5 means a classical pyramid, where each next layer is twice smaller than the previous one. # # * levels number of pyramid layers including the initial image; # levels=1 means that no extra layers are created and only the original images are used. # * winsize averaging window size # * larger values increase the algorithm robustness to image # * noise and give more chances for fast motion detection, but yield more blurred motion field. # * iterations number of iterations the algorithm does at each pyramid level. # * poly_n size of the pixel neighborhood used to find polynomial expansion in each pixel # * larger values mean that the image will be approximated with smoother surfaces, yielding more robust algorithm and more blurred motion field, typically poly_n =5 or 7. # * poly_sigma standard deviation of the Gaussian that is used to smooth derivatives used as a basis for the polynomial expansion; for poly_n=5, you can set poly_sigma=1.1, for poly_n=7, a good value would be poly_sigma=1.5. import cv2 import numpy as np # Capture the frame cap = cv2.VideoCapture(0) ret, frame1 = cap.read() # Get gray scale image of first frame and make a mask in HSV color prvsImg = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) # create a hsv mask hsv_mask = np.zeros_like(frame1) hsv_mask[:,:,1] = 255 # all x & y pts in 1 is saturation channel # get the sat channel and set it to 255 means fully saturated while True: ret, frame2 = cap.read() # get the frame nextImg = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) # convert to gray # Check out the markdown text above for a break down of these paramters, # these are suggested defaults flow = cv2.calcOpticalFlowFarneback(prvsImg,nextImg, None, 0.5, 3, 15, 3, 5, 1.2, 0) # use defaults # Color the channels based on the angle of travel # Pay close attention to your video, the path of the direction of flow will determine color! mag, ang = cv2.cartToPolar(flow[:,:,0], flow[:,:,1],angleInDegrees=True) # cnvrts cart cords to polar cords, cnvrt x, y coords to mag, ang hsv_mask[:,:,0] = ang/2 # get the hue chnl and div by 2 # this will reduce the hues nd chng the colros when v mv lft to rt hsv_mask[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) # normalize the val chnl # stretching the min & max to 0 to 255 # Convert back to BGR to show with imshow from cv bgr = cv2.cvtColor(hsv_mask,cv2.COLOR_HSV2BGR) cv2.imshow('frame2',bgr) k = cv2.waitKey(30) & 0xff if k == 27: break # Set the Previous image as the next iamge for the loop prvsImg = nextImg cap.release() cv2.destroyAllWindows()
In [ ]:
import cv2  
import numpy as np 

cap = cv2.VideoCapture(0) 
ret, frame1 = cap.read() 
prvsImg = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) 

hsv_mask = np.zeros_like(frame1) 
hsv_mask[:,:,1] = 255 

while True: 
    ret, frame2 = cap.read() 
    nextImg = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) 
    flow = cv2.calcOpticalFlowFarneback(prvsImg,nextImg, None, 0.5, 3, 15, 3, 5, 1.2, 0) 
    mag, ang = cv2.cartToPolar(flow[:,:,0], flow[:,:,1],angleInDegrees=True) 
    hsv_mask[:,:,0] = ang/2 
    hsv_mask[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) 
    bgr = cv2.cvtColor(hsv_mask,cv2.COLOR_HSV2BGR) 
    cv2.imshow('frame2',bgr) 
     
    k = cv2.waitKey(30) & 0xff 
    if k == 27: 
        break 
    prvsImg = nextImg 
cap.release() 
cv2.destroyAllWindows() 

MeanShift Tracking

# Mv frm left to rt vs rt to left vs up to below vs below to up import cv2 cv2.cartToPolar import numpy as np import cv2 # Capture a video stream cap = cv2.VideoCapture(0) # take first frame of the video ret,frame = cap.read() print(frame) # Set Up the Initial Tracking Window # first detect the face and set that as our starting box. face_cascade = cv2.CascadeClassifier('haarcascades\\haarcascade_frontalface_default.xml') face_rects = face_cascade.detectMultiScale(frame) # this gvs a list of np arrs # v only tk one 1 arr to detect face # prev v used corner dection now v r using obj detection # to get face loc, whi is a grp of pxls and apply MS tracking # in other words v r telling MS to detect face 1 time in the begining # nd then tell MS alg to track tht set of pxls print(face_rects) # Convert this list of a single array to a tuple of (x,y,w,h) (face_x,face_y,w,h) = tuple(face_rects[0]) # get the first face only track_window = (face_x,face_y,w,h) # this is a rect, then track it # set up the ROI for tracking roi = frame[face_y:face_y+h, face_x:face_x+w] #get roi # Use the HSV Color Mapping hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) # Find histogram to backproject the target on each frame for calculation # of meanshit roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180]) # this is hist # Normalize the histogram array values given a min of 0 and max of 255 cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX) # Setup the termination criteria, either 10 iterations or # move by at least 1 pt term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) # these r default vals, v can chng them while True: ret ,frame = cap.read() # if the frame is returnd if ret == True: # Grab the Frame in HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # Calculate the Back Projection based off the roi_hist created dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) # Apply meanshift to get the new coordinates of the rectangle ret, track_window = cv2.meanShift(dst, track_window, term_crit) # Draw the new rectangle on the image x,y,w,h = track_window img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255),5) cv2.imshow('img2',img2) k = cv2.waitKey(1) & 0xff if k == 27: break else: break cv2.destroyAllWindows() cap.release()
In [ ]:
import cv2 
cv2.cartToPolar 
 
import numpy as np 
import cv2  
 
cap = cv2.VideoCapture(0) 
 
ret,frame = cap.read() 
 
print(frame) 
In [ ]:
 
face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml') 
face_rects = face_cascade.detectMultiScale(frame)  
 
print(face_rects) 
(face_x,face_y,w,h) = tuple(face_rects[0])  
 
track_window = (face_x,face_y,w,h) 
roi = frame[face_y:face_y+h, face_x:face_x+w] 

hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) 
roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180]) 

cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX) 
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) 

while True: 
    ret ,frame = cap.read() 
    if ret == True: 
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 
        dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) 
        ret, track_window = cv2.meanShift(dst, track_window, term_crit) 
        x,y,w,h = track_window 
        img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255),5) 
        cv2.imshow('img2',img2) 
        k = cv2.waitKey(1) & 0xff 
        if k == 27: 
            break 
    else: 
        break 
         
cv2.destroyAllWindows() 
cap.release()  

Cam shift tracking:

import numpy as np import cv2 # Capture a video stream cap = cv2.VideoCapture(0) # take first frame of the video ret,frame = cap.read() # Set Up the Initial Tracking Window print(frame) # first detect the face and set that as our starting box. face_cascade = cv2.CascadeClassifier('haarcascades\\haarcascade_frontalface_default.xml') face_rects = face_cascade.detectMultiScale(frame) # Convert this list of a single array to a tuple of (x,y,w,h) (face_x,face_y,w,h) = tuple(face_rects[0]) track_window = (face_x,face_y,w,h) # set up the ROI for tracking roi = frame[face_y:face_y+h, face_x:face_x+w] # Use the HSV Color Mapping hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) # Find histogram to backproject the target on each frame for calculation of meanshit roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180]) # Normalize the histogram array values given a min of 0 and max of 255 cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX) # Setup the termination criteria, either 10 iteration or move by at least 1 pt term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) while True: ret ,frame = cap.read() if ret == True: # Grab the Frame in HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # Calculate the Back Projection based off the roi_hist created earlier dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) ########## CAM SHIFT #################################### # Apply Camshift to get the new coordinates of the rectangle ret, track_window = cv2.CamShift(dst, track_window, term_crit) # resize the rec based on the position of the face # if it is close draw a bigger one vs vice versa # Draw it on image pts = cv2.boxPoints(ret) pts = np.int0(pts) # convert em all to ints img2 = cv2.polylines(frame,[pts],True, (0,0,255),5) # red color cv2.imshow('img2',img2) ######################################################## k = cv2.waitKey(1) & 0xff if k == 27: break else: break cv2.destroyAllWindows() cap.release()
In [ ]:
import numpy as np 
import cv2  
cap = cv2.VideoCapture(0) 
ret,frame = cap.read() 

print(frame)# u can comment this

face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml') 
face_rects = face_cascade.detectMultiScale(frame)  

(face_x,face_y,w,h) = tuple(face_rects[0])  
track_window = (face_x,face_y,w,h) 

roi = frame[face_y:face_y+h, face_x:face_x+w] 
hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) 
roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180]) 
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX) 
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) 
 
while True: 
    ret ,frame = cap.read() 
    if ret == True: 
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 
        dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) 
        ret, track_window = cv2.CamShift(dst, track_window, term_crit) 

        pts = cv2.boxPoints(ret) 
        pts = np.int0(pts) 
        img2 = cv2.polylines(frame,[pts],True, (0,0,255),5) 
        cv2.imshow('img2',img2) 
        k = cv2.waitKey(1) & 0xff 
        if k == 27: 
            break 
         
    else: 
        break          
cv2.destroyAllWindows() 
cap.release() 
 
 

Other Tracking apis

def ask_for_tracker(): print("What Tracker API would you like to use?") print("Enter 0 for BOOSTING: ") print("Enter 1 for MIL: ") print("Enter 2 for KCF: ") print("Enter 3 for TLD: ") print("Enter 4 for MEDIANFLOW: ") choice = input("Please select your tracker: ") if choice == '0': tracker = cv2.TrackerBoosting_create() if choice == '1': tracker = cv2.TrackerMIL_create() if choice == '2': tracker = cv2.TrackerKCF_create() if choice == '3': tracker = cv2.TrackerTLD_create() if choice == '4': tracker = cv2.TrackerMedianFlow_create() return tracker tracker = ask_for_tracker() tracker_name = str(tracker).split()[0][1:] print(tracker_name) # Read video cap = cv2.VideoCapture(0) # Read first frame. ret, frame = cap.read() # Special function allows us to draw on the very first frame our desired ROI roi = cv2.selectROI(frame, False) # Initialize tracker with first frame and bounding box ret = tracker.init(frame, roi) while True: # Read a new frame ret, frame = cap.read() # Update tracker success, roi = tracker.update(frame) # roi variable is a tuple of 4 floats # We need each value and we need them as integers (x,y,w,h) = tuple(map(int,roi)) # Draw Rectangle as Tracker moves if success: # Tracking success p1 = (x, y) p2 = (x+w, y+h) cv2.rectangle(frame, p1, p2, (0,255,0), 3) else : # Tracking failure cv2.putText(frame, "Failure to Detect Tracking!!", (100,200), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),3) # Display tracker type on frame cv2.putText(frame, tracker_name, (20,400), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),3); # Display result cv2.imshow(tracker_name, frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27 : break cap.release() cv2.destroyAllWindows()
In [ ]:
# Other Tracking apis 
import cv2
def ask_for_tracker(): 
    print("What Tracker API would you like to use?") 
    print("Enter 0 for BOOSTING: ") 
    print("Enter 1 for MIL: ") 
    print("Enter 2 for KCF: ") 
    print("Enter 3 for TLD: ") 
    print("Enter 4 for MEDIANFLOW: ") 
    choice = input("Please select your tracker: ") 
     
    if choice == '0': 
        tracker = cv2.TrackerBoosting_create() 
    if choice == '1': 
        tracker = cv2.TrackerMIL_create() 
    if choice == '2': 
        tracker = cv2.TrackerKCF_create() 
    if choice == '3': 
        tracker = cv2.TrackerTLD_create() 
    if choice == '4': 
        tracker = cv2.TrackerMedianFlow_create() 
    return tracker 
 
tracker = ask_for_tracker() 
 
tracker_name = str(tracker).split()[0][1:] 
print(tracker_name) 
cap = cv2.VideoCapture(0) 
ret, frame = cap.read() 
roi = cv2.selectROI(frame, False) 
ret = tracker.init(frame, roi) 
while True: 
    ret, frame = cap.read()    
    success, roi = tracker.update(frame) 
    (x,y,w,h) = tuple(map(int,roi)) 
    if success: 
        # Tracking success 
        p1 = (x, y) 
        p2 = (x+w, y+h) 
        cv2.rectangle(frame, p1, p2, (0,255,0), 3) 
    else : 
        cv2.putText(frame, "Failure to Detect Tracking!!", (100,200), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),3) 
    cv2.putText(frame, tracker_name, (20,400), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),3); 
    cv2.imshow(tracker_name, frame) 
    k = cv2.waitKey(1) & 0xff 
    if k == 27 :  
        break 
         
cap.release() 
cv2.destroyAllWindows() 
In [ ]:
# Traffic Sign
import cv2 import numpy as np from scipy.stats import itemfreq def get_dominant_color(image, n_colors): pixels = np.float32(image).reshape((-1, 3)) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1) flags = cv2.KMEANS_RANDOM_CENTERS flags, labels, centroids = cv2.kmeans( pixels, n_colors, None, criteria, 10, flags) palette = np.uint8(centroids) '''print('palette') print(palette) print('palette[np.argmax(itemfreq(labels)[:, -1])]')''' print(palette[np.argmax(itemfreq(labels)[:, -1])]) return palette[np.argmax(itemfreq(labels)[:, -1])] clicked = False def onMouse(event, x, y, flags, param): global clicked if event == cv2.EVENT_LBUTTONUP: clicked = True cameraCapture = cv2.VideoCapture(0) cv2.namedWindow('camera') cv2.setMouseCallback('camera', onMouse) # Read and process frames in loop success, frame = cameraCapture.read() while success and not clicked: cv2.waitKey(1) success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) img = cv2.medianBlur(gray, 37) circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 50, param1=120, param2=40) if not circles is None: circles = np.uint16(np.around(circles)) max_r, max_i = 0, 0 for i in range(len(circles[:, :, 2][0])): if circles[:, :, 2][0][i] > 50 and circles[:, :, 2][0][i] > max_r: max_i = i max_r = circles[:, :, 2][0][i] x, y, r = circles[:, :, :][0][max_i] if y > r and x > r: square = frame[y-r:y+r, x-r:x+r] dominant_color = get_dominant_color(square, 2) if dominant_color[2] > 100: print("STOP") elif dominant_color[0] > 80: zone_0 = square[square.shape[0]*3//8:square.shape[0] * 5//8, square.shape[1]*1//8:square.shape[1]*3//8] cv2.imshow('Zone0', zone_0) zone_0_color = get_dominant_color(zone_0, 1) zone_1 = square[square.shape[0]*1//8:square.shape[0] * 3//8, square.shape[1]*3//8:square.shape[1]*5//8] cv2.imshow('Zone1', zone_1) zone_1_color = get_dominant_color(zone_1, 1) zone_2 = square[square.shape[0]*3//8:square.shape[0] * 5//8, square.shape[1]*5//8:square.shape[1]*7//8] cv2.imshow('Zone2', zone_2) zone_2_color = get_dominant_color(zone_2, 1) if zone_1_color[2] < 60: if sum(zone_0_color) > sum(zone_2_color): print("LEFT") else: print("RIGHT") else: if sum(zone_1_color) > sum(zone_0_color) and sum(zone_1_color) > sum(zone_2_color): print("FORWARD") elif sum(zone_0_color) > sum(zone_2_color): print("FORWARD AND LEFT") else: print("FORWARD AND RIGHT") else: print("N/A") for i in circles[0, :]: cv2.circle(frame, (i[0], i[1]), i[2], (0, 255, 0), 2) cv2.circle(frame, (i[0], i[1]), 2, (0, 0, 255), 3) cv2.imshow('camera', frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27 : break cv2.destroyAllWindows() cameraCapture.release()
In [ ]:
import cv2 
import numpy as np 
from scipy.stats import itemfreq 

def get_dominant_color(image, n_colors): 
    pixels = np.float32(image).reshape((-1, 3)) 
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1) 
    flags = cv2.KMEANS_RANDOM_CENTERS 
    flags, labels, centroids = cv2.kmeans( 
        pixels, n_colors, None, criteria, 10, flags) 
    palette = np.uint8(centroids) 
    print(palette[np.argmax(itemfreq(labels)[:, -1])]) 
    return palette[np.argmax(itemfreq(labels)[:, -1])] 

clicked = False 

def onMouse(event, x, y, flags, param): 
    global clicked 
    if event == cv2.EVENT_LBUTTONUP: 
        clicked = True 

cameraCapture = cv2.VideoCapture(0)  
cv2.namedWindow('camera') 
cv2.setMouseCallback('camera', onMouse) 

success, frame = cameraCapture.read() 
 
while success and not clicked: 
    cv2.waitKey(1) 
    success, frame = cameraCapture.read() 
 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    img = cv2.medianBlur(gray, 37) 
    circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 
                              1, 50, param1=120, param2=40) 
 
    if not circles is None: 
        circles = np.uint16(np.around(circles)) 
        max_r, max_i = 0, 0 
        for i in range(len(circles[:, :, 2][0])): 
            if circles[:, :, 2][0][i] > 50 and circles[:, :, 2][0][i] > max_r: 
                max_i = i 
                max_r = circles[:, :, 2][0][i] 
        x, y, r = circles[:, :, :][0][max_i] 
        if y > r and x > r: 
            square = frame[y-r:y+r, x-r:x+r] 
 
            dominant_color = get_dominant_color(square, 2) 
            if dominant_color[2] > 100: 
                print("STOP") 
            elif dominant_color[0] > 80: 
                zone_0 = square[square.shape[0]*3//8:square.shape[0] 
                                * 5//8, square.shape[1]*1//8:square.shape[1]*3//8] 
                cv2.imshow('Zone0', zone_0) 
                zone_0_color = get_dominant_color(zone_0, 1) 
 
                zone_1 = square[square.shape[0]*1//8:square.shape[0] 
                                * 3//8, square.shape[1]*3//8:square.shape[1]*5//8] 
                cv2.imshow('Zone1', zone_1) 
                zone_1_color = get_dominant_color(zone_1, 1) 
 
                zone_2 = square[square.shape[0]*3//8:square.shape[0] 
                                * 5//8, square.shape[1]*5//8:square.shape[1]*7//8] 
                cv2.imshow('Zone2', zone_2) 
                zone_2_color = get_dominant_color(zone_2, 1) 
 
                if zone_1_color[2] < 60: 
                    if sum(zone_0_color) > sum(zone_2_color): 
                        print("LEFT") 
                    else: 
                        print("RIGHT") 
                else: 
                    if sum(zone_1_color) > sum(zone_0_color) and sum(zone_1_color) > sum(zone_2_color): 
                        print("FORWARD") 
                    elif sum(zone_0_color) > sum(zone_2_color): 
                        print("FORWARD AND LEFT") 
                    else: 
                        print("FORWARD AND RIGHT") 
            else: 
                print("N/A") 
 
        for i in circles[0, :]: 
            cv2.circle(frame, (i[0], i[1]), i[2], (0, 255, 0), 2) 
            cv2.circle(frame, (i[0], i[1]), 2, (0, 0, 255), 3) 
    cv2.imshow('camera', frame) 
 # Exit if ESC pressed 
    k = cv2.waitKey(1) & 0xff 
    if k == 27 :  
        break 
cv2.destroyAllWindows() 
cameraCapture.release() 
 
 
In [ ]:
# Car detection
import cv2
print(cv2.__version__)
In [ ]:
import os
os.chdir('c:/Users/Hari/Desktop/datasets/Naresh_datasets/')
# cars.xml file will posses important role hare
cascade_src = 'cars.xml'
#load the video which has the cars moving
video_src = 'CarDetectionusingOpenCV.mp4'


cap = cv2.VideoCapture(video_src) # read the video
car_cascade = cv2.CascadeClassifier(cascade_src) # draw rectangls or mark or dectect the cars motions

# iterate loop 
while True:
    #tuple unpacking
    ret, img = cap.read()
    if(type(img) == type(None)):
        break
        
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    cars = car_cascade.detectMultiScale(gray,1.1,1)
    
    for (x,y,w,h) in cars:
        
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        
                

    
    cv2.imshow('video',img)
    if cv2.waitKey(33) == 27:
        break
        
        
cv2.destroyAllWindows()
    
In [ ]:
import os
os.chdir('c:/Users/Hari/Desktop/datasets/Naresh_datasets/')
# cars.xml file will posses important role hare
cascade_src = 'cars.xml'
#load the video which has the cars moving
# video_src = 'CarDetectionusingOpenCV.mp4'
video_src1 = 'VehicleDetection.mp4'

cap = cv2.VideoCapture(video_src1) # read the video
car_cascade = cv2.CascadeClassifier(cascade_src) # draw rectangls or mark or dectect the cars motions

# iterate loop 
while True:
    #tuple unpacking
    ret, img = cap.read()
    if(type(img) == type(None)):
        break
        
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    cars = car_cascade.detectMultiScale(gray,1.1,1)
    
    for (x,y,w,h) in cars:
        
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        
                

    
    cv2.imshow('video',img)
    if cv2.waitKey(33) == 27:
        break
        
        
cv2.destroyAllWindows()
    
In [11]:
import cv2
from PIL import Image
e_img = cv2.imread('c:/Users/Hari/Desktop/datasets/reverse_img.jpg')
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(e_img) # BY default matplotlib load in BGR-color space model
Out[11]:
<matplotlib.image.AxesImage at 0x14d33a1cc88>
In [5]:
img_l = Image.open('c:/Users/Hari/Desktop/datasets/reverse_img.jpg')
img_l
Out[5]:
In [6]:
import cv2
img_f = cv2.flip(e_img,-1)
plt.imshow(img_f)
Out[6]:
<matplotlib.image.AxesImage at 0x14d30fdeb08>
In [7]:
img_r = cv2.resize(img_f,(1200,1200))
plt.imshow(img_r)
Out[7]:
<matplotlib.image.AxesImage at 0x14d3104c548>
In [8]:
plt.figure(figsize=(12,8))
ax = plt.subplot(111)
ax.imshow(img_r)
Out[8]:
<matplotlib.image.AxesImage at 0x14d31061dc8>
In [9]:
import numpy as np
img_b = np.zeros((1200,1200,3),np.uint8)
plt.imshow(img_b)
Out[9]:
<matplotlib.image.AxesImage at 0x14d320c8a08>
In [10]:
import cv2
cv2.imshow('imgre',img_r)
cv2.waitKey(0)
Out[10]:
-1
In [12]:
im= Image.open('c:/Users/Hari/Desktop/datasets/reverse_img.jpg')
im
Out[12]:
In [15]:
from PIL import Image
im.transpose(Image.FLIP_TOP_BOTTOM)
Out[15]:
In [ ]:
from PIL import Image, ImageDraw
from math import floor

# Load image:
input_image = Image.open("input.png")
input_pixels = input_image.load()

new_size = (300, 300)

# Create output image
output_image = Image.new("RGB", new_size)
draw = ImageDraw.Draw(output_image)

x_scale = input_image.width / output_image.width
y_scale = input_image.height / output_image.height

# Copy pixels
for x in range(output_image.width):
    for y in range(output_image.height):
        xp, yp = floor(x * x_scale), floor(y * y_scale)
        draw.point((x, y), input_pixels[xp, yp])

output_image.save("output.png")
In [2]:
import numpy as np
np.empty((3,4))
Out[2]:
array([[6.23042070e-307, 1.11259940e-306, 7.56603882e-307,
        1.78019082e-306],
       [6.89810244e-307, 9.34604358e-307, 1.33511018e-306,
        1.33511969e-306],
       [1.24610383e-306, 1.06809792e-306, 8.34444713e-308,
        2.22507386e-306]])
In [ ]:
 
In [ ]: